在C#后台线程 [英] Background Threads in C#
问题描述
我是新来的C#。我了解到,通常所有线程都最前面,除非,除非您使用显式指定为后台线程的IsBackground = TRUE 。
有些疑惑突然出现在我的脑海里。
1)什么是保持一个线程作为后台线程?
的优势2)当执行如下因素code:
静态无效的主要(字串[] args)
{
螺纹工人=新主题(SayHello的);
worker.IsBackground = TRUE;
worker.Start();
Console.WriteLine(你好从主);
}
静态无效的SayHello()
{
Console.WriteLine(Hello World的);
Console.ReadKey(真正的);
}
我需要使用worker.Join(),以保持主线程等待,因为程序立即终止。除了加入()我可以使用其他技术,以保持主线程等待?
1)什么是保持优势 线程作为后台线程?
它的优点是后台线程不会从终止停止程序。在一个大的应用程序也可能是有点难以停用所有的线程,如果你想退出应用程序。
除了加入()我可以使用其他技术,以保持主线程等待?
如果哟想使主程序等待你为什么使线程摆在首位,然后在后台线程??? 除了中加入(),你可以使用的EventWaitHandle或监视器,使主要方法等。
I am new to C#.I learnt that normally all threads are foreground until unless you explicitly specify it as "background" thread using IsBackGround= true .
Some doubts popped in to my mind.
1) What is the advantage of keeping a thread as background thread?
2) When executing the folowing code :
static void Main(string[] args)
{
Thread worker = new Thread(SayHello);
worker.IsBackground = true;
worker.Start();
Console.WriteLine("Hello From Main");
}
static void SayHello()
{
Console.WriteLine("Hello World");
Console.ReadKey(true);
}
i need to use worker.Join() to keep the main thread to wait as the program terminates immediately. Apart from Join() could i use other techniques to keep the main thread wait ?
1) What is the advantage of keeping a thread as background thread?
The Advantage is that a Background Thread doesn't stop the Program from terminating. In a large Application it could be a bit hard to deactivate all threads if you want to quit the Application.
Apart from Join() could i use other techniques to keep the main thread wait?
If yo want to make the Main program wait why do you make the thread a background thread in the first place then??? Besides of Join() you could use a EventWaitHandle or Monitor to make the main method wait.
这篇关于在C#后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!