异步方法引发异常 [英] Async Method throws Exception

查看:58
本文介绍了异步方法引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用引发异常的方法时遇到一些问题,但是我不确定为什么.异常使我的应用程序崩溃.

I'm currently having some problems with a method that throws an exception but I'm not sure why. The exception makes my application crash.

System.NullReferenceException: Object reference not set to an instance of an object.  
   at Myapp.AutoProcess.<ToRead>d__36.MoveNext()  
--- End of stack trace from previous location where exception was thrown ---  
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(Object state)  
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)   
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,    ContextCallback callback, Object state, Boolean preserveSyncCtx)  
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback   callback, Object state, Boolean preserveSyncCtx)  
   at   System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()  
   at System.Threading.ThreadPoolWorkQueue.Dispatch() 
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

我在单独的线程上运行该方法

I run the method on a separate thread

Thread listenerThread = new Thread(() => ToRead());
listenerThread.Start();

引发异常的方法如下:

private async void ToRead()
{
    while (true)
    {
        if (this.toRead.Count != 0)
        {
            string protocol = this.toRead[0];
            string[] temp = protocol.Split(',');
            string message = temp[0];
            string UserName = temp[1];
            Process(message, UserName);
            this.toRead.RemoveAt(0);
        }
        await Task.Delay(200);
    }
}

它从列表中接收传入的消息,并过滤出用户名和消息以将其发送到Process方法.如果有人可以帮助我,我将不胜感激.

It takes incoming messages from a List and filters out the Username and Message to send it to the Process method. I would appreciate if someone could help me out.

注意:该异常大约每天在Windows R2 2008 Server上运行一次.因此,我无法在Visual Studio中真正对其进行调试

Note: The exception occurs about once a day running it on a Windows R2 2008 Server. Therefore I cant really debug it in Visual Studio

推荐答案

您看到该异常使您的进程崩溃,因为您使用的是 async void 方法.

You're seeing that exception crash your process because you're using an async void method.

使用任务代替这样的线程:

Instead of using a thread, use a task, as such:

private async Task ToReadAsync()

Task listenerTask = Task.Run(() => ToReadAsync());

现在,您将可以在 listenerTask.Exception 中很好地检索该异常.但这可能不会给您更多细节.

And now you'll be able to nicely retrieve the exception in listenerTask.Exception. But it probably won't give you much more detail.

可能发生的事情是,您的 toRead 变量在某个时刻被设置为 null .当前代码的整个概念是错误的.像这样的轮询绝对绝对不是从一个线程向另一个线程发送数据的方法.查看 BlockingCollection 或类似方法以获取正确的方法.

What's probably happening is that your toRead variable is set to null at some point. The entire concept of the current code is wrong; polling like this is most definitely not the way to send data from one thread to another. Check out BlockingCollection or something like that for a proper approach.

这篇关于异步方法引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆