发生主线程什么在这种情况下 [英] What happens to the main thread in this case
问题描述
我有以下的多线程程序:
类节目{
静态无效的主要(){
(新的Thread(DoSomething的))启动()。
}
静态无效DoSomething的(){
//球的东西在这里...
}
}
几个问题:
- 请问纺纱后的主线程退出关子线程
- 如果它退出,子线程是一个后台线程:是的主要过程将退出还是会等待后台线程完成李? >
通常情况下,如果要等待子线程完成,你会添加一个 x.Join();
行(其中x是你的线程的变量),无论你想你的主线程停下来,等待孩子来完成。
编辑:所以,是的,主线程将退出,除非以下三种情况之一发生:
A)主线程代码的其余部分之前,催生线程完成(如果您添加任何)
b)你有等待线程完成(比如我提到的加入语句的条件,但也有其他的方式太)
三)主线程进入一个半无限循环(如游戏/图形引擎)。
在您的裸机例如,它将退出肯定(给你的问题的参数,在后台线程)
EDIT2:对不起,我似乎已经躲过你的第二个问题(实际上只考虑后台线程整个时间)。如果它是一个后台线程将退出,因为我已经解释了,如果它的前景那么它不应该(虽然我没有与前台线程太多的经验,所以我不能肯定地说)。
因此,要回答你的问题:是的,主线程退出。是的,如果孩子是专门后台线程,进程将退出以及
EDIT3:最后的编辑,我保证。我只是想添加一些代码,让你可以证明自己的答案(这是总是很高兴能够做到):
静态无效的主要(字串[] args)
{
螺纹纤维性=新主题(DoSomething的);
thready.IsBackground = TRUE;
thready.Start();
}
静态无效DoSomething的()
{
,而(真)
{
Console.Write(线程的循环\\\
);
}
}
通过翻转纤维性。的IsBackground = TRUE;
到 thready.IsBackground = FALSE;
你运行一个永远的计划(不退出,直到线程执行)。把它当作真正的退出速度非常快。
I have the following multithreaded program:
class Program{
static void main(){
(new Thread(DoSomething)).Start();
}
static void DoSomething(){
// Dome something here...
}
}
A couple of questions:
- Does the main thread exit after spinning off the child thread?
- If it does exit and the child thread is a background thread: Is the main process going to exit or will it wait for the background thread to finish?
Typically, if you want to wait for the child thread to complete, you'd add a x.Join();
line (where x is the variable of your thread) wherever you want your main thread to stop and wait for the child to complete.
EDIT: So, yes, the main thread will exit unless one of three cases occurs: a) the spawned thread completes before the rest of the main thread code (if you add any)
b) You have a condition that waits for the thread to complete (such as the Join statement I mentioned, but there are other ways too).
c) The main thread goes into a semi-infinite loop (such as a game/graphics engine).
In your bare-bones example, it will exit for sure (given your question's parameters, a background thread).
EDIT2: Sorry, I seem to have dodged your second question (and actually only considered background threads the whole while). If it's a background thread it will exit as I've explained, if it's foreground then it shouldn't (though I don't have much experience with foreground threads, so I can't say for sure).
So to answer your questions: Yes, the main thread exits. Yes, if the child is specifically a background thread, the process will exit as well.
EDIT3: Final edit, I promise. I just wanted to add some code so that you could prove the answer yourself (which is always nice to be able to do):
static void Main(string[] args)
{
Thread thready = new Thread(DoSomething);
thready.IsBackground = true;
thready.Start();
}
static void DoSomething()
{
while (true)
{
Console.Write("thread's looping \n");
}
}
By toggling the thready.IsBackground = true;
to thready.IsBackground = false;
you get a program that runs forever (not exiting until the thread does). Leaving it as true will exit very quickly.
这篇关于发生主线程什么在这种情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!