离开前调用Thread.Join() [英] Calling Thread.Join() before leaving

查看:80
本文介绍了离开前调用Thread.Join()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




假设我的Main函数看起来像这样:


static void Main(string [] args)

{


动作h1 =新动作(0);

线程hilo1 =新线程(新线程创建(h1.go)) ;

hilo1.Start();

}


如果我创建的主题没有完成,会发生什么主线程

退出Main()?


建议在退出Main()之前调用hilo1.Join()吗?


通常如何处理这件事? O :-)


谢谢!

解决方案

如果线程是后台线程,它将是使用

申请终止。


如果它不是后台线程,则该过程将一直存在,直到该线程为

返回。


要将其指示为后台线程,请在线程对象上将属性IsBackground设置为true




-

Patrik L?wendahl [C#MVP]
www.cshrp.net - 诙谐的程序员优雅代码

"FernandoRodríguez" < FE ******************* @ fernando-rodriguez.com>写在

消息新闻:8d ******************************** @ 4ax.com ...



假设我的Main函数看起来像这样:

static void Main(string [] args)
{

动作h1 =新动作(0);

线程hilo1 =新线程(新的ThreadStart(h1.go));
hilo1.Start ();
}

当主要
线程退出Main()时,我创建的线程还没有完成会发生什么?

是否可以在退出Main()之前调用hilo1.Join()?

通常如何处理此事? O :-)

谢谢!





" Patrik L?wendahl [C#MVP ]" < PA ************** @ csharpsweden.com>写在

消息新闻:eW ************** @ tk2msftngp13.phx.gbl ...

如果线程是后台线程,它将以
应用程序终止。

如果它不是后台线程,则该过程将一直存在,直到线程
返回。

要将它指示为后台线程,请在线程对象上将属性IsBackground设置为
true。




我即将回应IsBackground只能在线程

启动之前设置,但你绝对正确,它是一个完全可变的属性。

这引出了我以下问题:是以下程序

保证终止?


使用System;

使用System.Threading;


class Background {

public static void Main(){

Background b = new Background();

Thread t = new Thread(new ThreadStart(b.DoSleep));

t.Start();
}


public void DoSleep(){

Thread.Sleep(5000);

主题.CurrentThread.IsBackground = true;

Thread.CurrentThread.IsBackground = false;

while(true){}

}


}


对于我来说它作为一个用.NET 1.1构建的控制台程序终止了,但是我没有b $ b在文档中看到任何保证,当一个线程设置为后台时,将检查所有线程的状态,并终止

如果没有前景线程。


我承认这主要是学术兴趣,因为我无法想象

依赖于此行为而不是做一些更直接的事情。


理论上,


框架调用Abort on所有后台线程,当没有更多

前景线程时。因此理论上它会永远终止,因为当你将那一个标记为后台线程时,没有更多的前景线程。


-

Patrik L?wendahl [C#MVP]
www.cshrp.net - 诙谐的程序员优雅代码


" Mike Schilling" < MS ************* @ hotmail.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...


Patrik L?wendahl [C#MVP]" < PA ************** @ csharpsweden.com>在
消息新闻中写道:eW ************** @tk2msftngp13.phx.gbl ...

如果线程是后台线程,那么将被
申请终止。

如果它不是后台主题,则该过程将一直有效,直到主题
返回。

要将它指示为后台线程,请在线程对象上将属性IsBackground设置为
true。



我即将回应IsBackground只能在线程<之前设置br />开始了,但你是绝对正确的,它是一个完全可变的属性。
这引出了我以下问题:以下程序是否保证终止?

使用System;
使用System.Threading;

类背景{
public static void Main(){
background b = new Background();
线程t =新线程(新的ThreadStart(b.DoSleep));
t.Start();
}

public void Do Sleep(){
Thread.Sleep(5000);
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.IsBackground = false;
while(true){}
}


对于我来说,它确实作为一个用.NET 1.1构建的控制台程序终止,但我没有看到任何在文档中保证,当一个线程设置为后台时,将检查所有线程的状态,如果没有前台线程,则会终止。
我承认这在很大程度上符合学术兴趣,因为我无法想象它是否有必要依赖这种行为,而不是做更直接的事情。



Hi,

Assume I have Main function that looks like this:

static void Main(string[] args)
{

Action h1 = new Action(0);
Thread hilo1 = new Thread( new ThreadStart( h1.go ));
hilo1.Start();
}

What will happen if the thread I created hasn''t finished when the main thread
exits Main()?

Is it advisable to call hilo1.Join() before exiting Main() ?

How''s this thing handled usually? O:-)

Thanks!

解决方案

If the thread is a background thread, it will be terminated with the
application.

If it''s not a background thread, the process will live until the thread
returns.

To indicate it as a background thread, set the property IsBackground to true
on the thread object.

--
Patrik L?wendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"
"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:8d********************************@4ax.com...

Hi,

Assume I have Main function that looks like this:

static void Main(string[] args)
{

Action h1 = new Action(0);
Thread hilo1 = new Thread( new ThreadStart( h1.go ));
hilo1.Start();
}

What will happen if the thread I created hasn''t finished when the main
thread
exits Main()?

Is it advisable to call hilo1.Join() before exiting Main() ?

How''s this thing handled usually? O:-)

Thanks!




"Patrik L?wendahl [C# MVP]" <pa**************@csharpsweden.com> wrote in
message news:eW**************@tk2msftngp13.phx.gbl...

If the thread is a background thread, it will be terminated with the
application.

If it''s not a background thread, the process will live until the thread
returns.

To indicate it as a background thread, set the property IsBackground to
true on the thread object.



I was about to respond that IsBackground can only be set before the thread
is started, but you''re absolutely right, it''s a fully mutable property.
Which leads me to the following question: is the following program
guaranteed to terminate?

using System;
using System.Threading;

class Background {
public static void Main() {
Background b = new Background();
Thread t = new Thread(new ThreadStart(b.DoSleep));
t.Start();
}

public void DoSleep() {
Thread.Sleep(5000);
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.IsBackground = false;
while (true) { }
}

}

It does terminate, for me, as a console program built with .NET 1.1, but I
don''t see any guarantee in the documentation that, at the moment a thread is
set to background, the state of all threads will be checked, and termination
will occur if no foreground threads remain.

I admit that this is largely of academic interest, since I can''t picture it
ever being necessary to depend upon this behavior rather than doing
something more straightforward.


Theoreticly,

The framwork calls Abort on all background threads when there''s no more
foreground threads. So theoreticly it would always terminate since there''s
no more foreground threads when you mark that one as a background thread.

--
Patrik L?wendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

"Mike Schilling" <ms*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


"Patrik L?wendahl [C# MVP]" <pa**************@csharpsweden.com> wrote in
message news:eW**************@tk2msftngp13.phx.gbl...

If the thread is a background thread, it will be terminated with the
application.

If it''s not a background thread, the process will live until the thread
returns.

To indicate it as a background thread, set the property IsBackground to
true on the thread object.



I was about to respond that IsBackground can only be set before the thread
is started, but you''re absolutely right, it''s a fully mutable property.
Which leads me to the following question: is the following program
guaranteed to terminate?

using System;
using System.Threading;

class Background {
public static void Main() {
Background b = new Background();
Thread t = new Thread(new ThreadStart(b.DoSleep));
t.Start();
}

public void DoSleep() {
Thread.Sleep(5000);
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.IsBackground = false;
while (true) { }
}

}

It does terminate, for me, as a console program built with .NET 1.1, but I
don''t see any guarantee in the documentation that, at the moment a thread
is set to background, the state of all threads will be checked, and
termination will occur if no foreground threads remain.

I admit that this is largely of academic interest, since I can''t picture
it ever being necessary to depend upon this behavior rather than doing
something more straightforward.



这篇关于离开前调用Thread.Join()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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