当创建它的主线程在 c# 中被杀死时,任务是否仍然存在? [英] Does the task still survive when the main thread that creates it has been killed in c#?

查看:35
本文介绍了当创建它的主线程在 c# 中被杀死时,任务是否仍然存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下方法创建任务的主线程:

I have a main thread that creates a task using:

new TaskFactory(cancellationToken).StartNew(() => DoSomething(cancellationToken), 
                                            TaskCreationOptions.LongRunning);

我想知道在c#中创建任务的主线程被杀死后任务是否仍然存在.

I wonder if the task still survive when the main thread that creates the task has been killed in c#.

推荐答案

如果进程的主线程被终止,进程将中止.当一个进程被终止时,所有线程(和其他拥有的资源)也会被关闭/终止/终止.

If a process' main thread is terminated, the process aborts. When a process is killed, all threads (and other owned resources) are closed/terminated/killed also.

因此,如果您进程的主线程创建了一个工作线程并且主线程被杀死,那么工作线程也会终止.

Therefore, if your process' main thread created a worker thread and the main thread is killed, the worker thread is also terminated.

不要被 TaskCreationOptions.LongRunning 枚举所迷惑——这只是向底层线程池管理器表明它可能想要在最大线程的正常约束之外创建这个线程——池大小.

Don't be confused by the TaskCreationOptions.LongRunning enum - this is just an indicator to the underlying thread-pool manager that it might want to create this thread outside the normal constraints on maximum thread-pool size.

这篇关于当创建它的主线程在 c# 中被杀死时,任务是否仍然存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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