为什么出现异常System.Threading.Tasks.TaskCanceledException

本文关键字:Threading Tasks TaskCanceledException System 异常 为什么 | 更新日期: 2023-09-27 18:21:39

我正在构建一个xamarin android应用程序,但不能使用异步等待。

执行停滞很长一段时间,然后最终抛出异常,消息为:System.Threading.Tasks.TaskCanceledException:任务已取消

首先我觉得它被卡住了,因为我没有使用ConfigureAwait(false),但使用它并不能解决任何问题。

代码:

protected override async void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    await ProcessData().ConfigureAwait(false); 
}
private async Task ProcessData()
{
    await Task.Delay(new TimeSpan(0, 0, 2)).ConfigureAwait(false); // 2 seconds
}

堆栈跟踪:

Thread started: <Thread Pool> #6
11-11 15:11:06.144 W/        (19542): _wapi_connect: error looking up socket handle 0x26
11-11 15:11:06.177 D/Mono    (19542): DllImport attempting to load: '/system/lib/liblog.so'.
11-11 15:11:06.177 D/Mono    (19542): DllImport loaded library '/system/lib/liblog.so'.
11-11 15:11:06.177 D/Mono    (19542): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
11-11 15:11:06.177 D/Mono    (19542): Searching for '__android_log_print'.
11-11 15:11:06.177 D/Mono    (19542): Probing '__android_log_print'.
11-11 15:11:06.177 D/Mono    (19542): Found as '__android_log_print'.
11-11 15:11:06.178 I/MonoDroid(19542): UNHANDLED EXCEPTION:
11-11 15:11:06.179 I/MonoDroid(19542): System.Threading.Tasks.TaskCanceledException: A task was canceled.
11-11 15:11:06.179 I/MonoDroid(19542): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
11-11 15:11:06.179 I/MonoDroid(19542): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0003b>
11-11 15:11:06.179 I/MonoDroid(19542): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () <IL 0x0000c, 0x00032>
11-11 15:11:06.179 I/MonoDroid(19542): at Java.Lang.Thread/RunnableImplementor.Run () <IL 0x00011, 0x0004c>
11-11 15:11:06.179 I/MonoDroid(19542): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <IL 0x0000a, 0x0005e>
11-11 15:11:06.179 I/MonoDroid(19542): at (wrapper dynamic-method) object.b257f262-40d0-43f7-b8b4-43d0ec65bc72 (intptr,intptr) <IL 0x00011, 0x0001f>
11-11 15:11:06.186 W/art     (19542): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
11-11 15:11:06.188 D/AndroidRuntime(19542): Shutting down VM
An unhandled exception occured.
11-11 15:12:58.748 E/AndroidRuntime(19542): FATAL EXCEPTION: main
11-11 15:12:58.748 E/AndroidRuntime(19542): Process: AndroidConsumer.AndroidConsumer, PID: 19542
11-11 15:12:58.748 E/AndroidRuntime(19542): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
11-11 15:12:58.748 E/AndroidRuntime(19542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-11 15:12:58.748 E/AndroidRuntime(19542): Caused by: java.lang.reflect.InvocationTargetException
11-11 15:12:58.748 E/AndroidRuntime(19542):     at java.lang.reflect.Method.invoke(Native Method)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at java.lang.reflect.Method.invoke(Method.java:372)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-11 15:12:58.748 E/AndroidRuntime(19542):     ... 1 more
11-11 15:12:58.748 E/AndroidRuntime(19542): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.Threading.Tasks.TaskCanceledException: A task was canceled.
11-11 15:12:58.748 E/AndroidRuntime(19542): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
11-11 15:12:58.748 E/AndroidRuntime(19542): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0003b>
11-11 15:12:58.748 E/AndroidRuntime(19542): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () <IL 0x0000c, 0x00032>
11-11 15:12:58.748 E/AndroidRuntime(19542): at Java.Lang.Thread/RunnableImplementor.Run () <IL 0x00011, 0x0004c>
11-11 15:12:58.748 E/AndroidRuntime(19542): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <IL 0x0000a, 0x0005e>
11-11 15:12:58.748 E/AndroidRuntime(19542): at (wrapper dynamic-method) object.b257f262-40d0-43f7-b8b4-43d0ec65bc72 (intptr,intptr) <IL 0x00011, 0x0001f>
11-11 15:12:58.748 E/AndroidRuntime(19542): 
11-11 15:12:58.748 E/AndroidRuntime(19542):     at mono.java.lang.RunnableImplementor.n_run(Native Method)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at android.os.Handler.handleCallback(Handler.java:739)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at android.os.Looper.loop(Looper.java:135)
11-11 15:12:58.748 E/AndroidRuntime(19542):     at android.app.ActivityThread.main(ActivityThread.java:5253)
11-11 15:12:58.748 E/AndroidRuntime(19542):     ... 4 more
11-11 15:12:58.877 I/Process (19542): Sending signal. PID: 19542 SIG: 9

为什么出现异常System.Threading.Tasks.TaskCanceledException

你能把它添加到你的代码中,然后把消息作为注释说出来吗。

try 
{
    await Task.Delay(new TimeSpan(0, 0, 2)).ConfigureAwait(false);
}
catch (System.Threading.Tasks.TaskCanceledException ex)
{
    MessageBox.Show(ex.Message);
}

在catch循环中,您可以使用任何想要处理错误消息的方法,只要您能够查看它。此外,将所有这些放入一个try-and-catch循环中是值得的,这样您就可以检查哪个部分出现了错误。