我承认这在很大程度上符合学术兴趣,因为我无法想象它是否有必要依赖这种行为,而不是做更直接的事情。 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 threadexits Main()?Is it advisable to call hilo1.Join() before exiting Main() ?How''s this thing handled usually? O:-)Thanks! 解决方案I was about to respond that IsBackground can only be set before the threadis started, but you''re absolutely right, it''s a fully mutable property.Which leads me to the following question: is the following programguaranteed 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 Idon''t see any guarantee in the documentation that, at the moment a thread isset to background, the state of all threads will be checked, and terminationwill occur if no foreground threads remain.I admit that this is largely of academic interest, since I can''t picture itever being necessary to depend upon this behavior rather than doingsomething more straightforward. 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 19:03