Prev: Stop Windows Service
Next: How does initializing inherited members inside base class constructor reduce the calls to…?
From: Willem van Rumpt on 18 Jun 2010 05:23 On 18-6-2010 9:15, Tony Johansson wrote: > > 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 > It will and it does. If you step through the program you will see exactly what the statement "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" Means: An exception thrown at s.Join(), and a hang on Thread.CurrentThread.Join()
From: Arne Vajhøj on 20 Jun 2010 22:10 On 18-06-2010 03:15, Tony Johansson wrote: > "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 If you add the missing: s.Start(); then it will hang! Arne
From: Peter Duniho on 21 Jun 2010 01:20 Arne Vajh�j wrote: > [...] >>> 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. [...] > > If you add the missing: > > s.Start(); > > then it will hang! It will hang regardless, and not on the "s.Join()" statement (which will throw an exception in the original code as intended, or will simply proceed normally when the started thread has exited as proposed by you�either way, no hang). The call to Join() on the CurrentThread cannot ever return, because the CurrentThread cannot exit until the call to Join() returns. Pete
From: Arne Vajhøj on 21 Jun 2010 22:42
On 21-06-2010 01:20, Peter Duniho wrote: > Arne Vajh�j wrote: >> [...] >>>> 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. [...] >> >> If you add the missing: >> >> s.Start(); >> >> then it will hang! > > It will hang regardless, You are correct. It catches that exception and continue. Arne |