小编Tim*_*995的帖子

异步方法抛出异常

我目前在使用抛出异常的方法时遇到了一些问题,但我不确定原因.该异常使我的应用程序崩溃.

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()
Run Code Online (Sandbox Code Playgroud)

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

Thread listenerThread = new Thread(() => ToRead());
listenerThread.Start();
Run Code Online (Sandbox Code Playgroud)

抛出异常的方法如下所示:

private async void ToRead()
{
    while (true)
    {
        if (this.toRead.Count != 0)
        { …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading asynchronous task

-2
推荐指数
1
解决办法
7557
查看次数

标签 统计

.net ×1

asynchronous ×1

c# ×1

multithreading ×1

task ×1