简体   繁体   English

在 C# 中使用 Async/Await 时,程序退出时是否会自动终止任务?

[英]When using Async/Await in C# are tasks terminated automatically when program exits?

If I have called multiple async functions that are still being awaited, and the program exits on the main thread, are the tasks still being awaited cleaned up automagically?如果我调用了多个仍在等待的异步函数,并且程序在主线程上退出,是否仍在等待自动清理任务? Or, do I need to ensure tasks are manually cleaned up when the program exits?或者,我是否需要确保在程序退出时手动清理任务?

Asking specifically when calling async functions, not using new Thread() .在调用异步函数时特别询问,而不是使用new Thread()

Also, in this specific case, Task is referring to System.Threading.Tasks.Task generated when calling an async function and awaiting a result.此外,在这种特定情况下, Task指的是调用async function 并等待结果时生成的System.Threading.Tasks.Task

Can't seem to find the answer in MSDN (unless I'm looking in the wrong place).似乎在 MSDN 中找不到答案(除非我找错地方了)。

are the tasks still being awaited cleaned up automagically?还在等待自动清理的任务吗?

If the tasks are awaited, the program is not going to exit before the tasks are completed.如果等待任务,则程序不会在任务完成之前退出。 So if the program exits, this means that the tasks are not awaited.所以如果程序退出,这意味着任务没有等待。 Or that they are awaited inside a task which is itself not awaited.或者他们在一个本身没有等待的任务中等待。 Or that they are awaited inside an async void method, which is not awaited by definition (an async void method returns nothing that can be awaited).或者它们在async void方法中等待,根据定义,该方法不等待( async void方法不返回任何可以等待的内容)。

So I am assuming that you are dealing with non-awaited tasks, or in other words with fire-and-forget tasks.所以我假设您正在处理未等待的任务,或者换句话说,处理的是即发即弃的任务。 These tasks in most cases are running on threads having the IsBackground property equal to true , or not running on any thread at all.在大多数情况下,这些任务在IsBackground属性等于true的线程上运行,或者根本不在 任何线程上运行。 These tasks will be terminated automatically, at an arbitrary execution point.这些任务将在任意执行点自动终止。 It is not impossible however to start a task that runs on a foreground thread, in which case the program will terminate when the task completes, or when the program is killed from the task manager.然而,启动在前台线程上运行的任务并非不可能,在这种情况下,程序将在任务完成时终止,或者当程序从任务管理器中终止时终止。

https://medium.com/@hasangi/lets-talk-about-parallelism-in-net-baby-2a69ba53f986 https://medium.com/@hasangi/lets-talk-about-parallelism-in-net-baby-2a69ba53f986

Threads are created as foreground processes by default(can be set to run in the background) and Tasks are created as background processes.线程默认创建为前台进程(可设置为在后台运行),任务创建为后台进程。 An application will not terminate when any foreground processes are running.当任何前台进程正在运行时,应用程序不会终止。 However, an application will terminate when all foreground processes complete even though when background processes are running.但是,即使后台进程正在运行,应用程序也会在所有前台进程完成后终止。 Hence a Task will terminate even without completing.因此,即使没有完成,任务也会终止。

If we can't create a task running in a foreground thread then the answer is yes, non-awaited tasks will always be terminated on program exit.如果我们不能创建在前台线程中运行的任务,那么答案是肯定的,未等待的任务将始终在程序退出时终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM