Prev: Stop Windows Service
Next: How does initializing inherited members inside base class constructor reduce the calls to…?
From: Tony Johansson on 17 Jun 2010 18:14 Hello! As I understand the join method on a thread object you must have at least two threads where one thread is waiting for another thread to complete this mean calling join on the main thread will cause the main thread to hang waiting for itself which is not possible. So is my understanding correct here ? //Tony
From: Arne Vajhøj on 17 Jun 2010 20:27 On 17-06-2010 18:14, Tony Johansson wrote: > As I understand the join method on a thread object you must have at least > two threads where one thread is waiting for another thread to complete this > mean calling join on the main thread will cause the main thread to hang > waiting for itself which is not possible. > > So is my understanding correct here ? t.Join() causes the current thread to wait until t has terminated. I have not tested what happen if t=current thread - either it will hang forever or you will get an exception because Join actually checks. Given that you should never do this, then it is not so interesting. Arne
From: Mike Schilling on 18 Jun 2010 02:27 "Arne Vajh�j" <arne(a)vajhoej.dk> wrote in message news:4c1abd86$0$286$14726298(a)news.sunsite.dk... > On 17-06-2010 18:14, Tony Johansson wrote: >> As I understand the join method on a thread object you must have at least >> two threads where one thread is waiting for another thread to complete >> this >> mean calling join on the main thread will cause the main thread to hang >> waiting for itself which is not possible. >> >> So is my understanding correct here ? > > t.Join() causes the current thread to wait until t has > terminated. > > I have not tested what happen if t=current thread - either > it will hang forever or you will get an exception because > Join actually checks. > Oddly, while Join checks for threads that have not been started, it does not check for the current thread, and will in fact hang forever. using System; using System.Threading; namespace Tests { class TestJoin { public static void Main() { try { Thread s = new Thread(new ThreadStart(doIt)); s.Join(); } catch (Exception ex) { Console.WriteLine(ex); } Thread.CurrentThread.Join(); } static void doIt() { } } }
From: Peter Duniho on 18 Jun 2010 02:47 Mike Schilling wrote: > [...] > Oddly, while Join checks for threads that have not been started, it does > not check for the current thread, and will in fact hang forever. Indeed. I have even seen code that uses that fact intentionally to keep a thread alive. The "technique" is to call Thread.Join() after the thread's done whatever setup it needs to. Then it blocks there until some other thread aborts it with the Thread.Abort() method. Not a code design I'd condone, but it's been done. :)
From: Tony Johansson on 18 Jun 2010 03:15
"Mike Schilling" <mscottschilling(a)hotmail.com> skrev i meddelandet news:eJDfd9qDLHA.4400(a)TK2MSFTNGP05.phx.gbl... > > > "Arne Vajh�j" <arne(a)vajhoej.dk> wrote in message > news:4c1abd86$0$286$14726298(a)news.sunsite.dk... >> On 17-06-2010 18:14, Tony Johansson wrote: >>> As I understand the join method on a thread object you must have at >>> least >>> two threads where one thread is waiting for another thread to complete >>> this >>> mean calling join on the main thread will cause the main thread to hang >>> waiting for itself which is not possible. >>> >>> So is my understanding correct here ? >> >> t.Join() causes the current thread to wait until t has >> terminated. >> >> I have not tested what happen if t=current thread - either >> it will hang forever or you will get an exception because >> Join actually checks. >> > > Oddly, while Join checks for threads that have not been started, it does > not check for the current thread, and will in fact hang forever. > > using System; > using System.Threading; > > namespace Tests > { > class TestJoin > { > public static void Main() > { > try > { > Thread s = new Thread(new ThreadStart(doIt)); > s.Join(); > } > catch (Exception ex) > { > Console.WriteLine(ex); > } > > Thread.CurrentThread.Join(); > } > > static void doIt() > { > } > } > } The code that you passed do not cause the Thread to hang you will catch the exception in the exception clause. I don't understand what you mean with "Oddly, while Join checks for threads that have not been started, it does not check for the current thread, and will in fact hang forever" But in fact as I wrote it wil not hang //Tony |