我目前在使用抛出异常的方法时遇到了一些问题,但我不确定原因.该异常使我的应用程序崩溃.
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)