Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Joining a thread
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01049204
Message ID:
01049391
Vues:
16
Chad,
I am already using the return value from the Join(timeOut) method and I have seen the output indicated in my original post even when the thread exists before the timeOut. That is what I found odd about the whole thing. In the thread code I check to see if the main thread has reached the timeOut, if it has then the while loop ends and the thread will exit gracefully shortly therafter.
I might build a small little app to test this in a contolled environment, I will try to remember to post the code.

It is just odd, but it works the way (I think) it should.

Einar

>Einar,
>
>When you join a thread with a timeout parameter, the join method returns a boolean value. That value is true if the thread terminated while you were joined to it and false if the timeout expired before the thread terminated. Either way, control goes back to the program and your code continues. So, I could see your output happening due to the fact that the timeout time elapsed after the thread made its third loop (Counter:2) even though the thread wasn't done processing. So, now your main thread continues running the code after the call to join the secondary thread while the secondary thread continues to run. You main thread ends so you see the exit code output and then the secondary thread does one more loop before it dies.
>
>HTH,
>Chad
>
>PS. You could check the return value of the join call to see if the thread you joined terminated while you were joined to it or not.
>
>
>>Chad,
>>It is acctually very simple, I only start one thread at the time and I join the thread after a relatively short period of time. Something like this:
>>
>>
>>start thread
>>do some stuff
>>join thread with timeout
>>
>>
>>The thread code is something like this:
>>
>>int Counter = 0;
>>while (some_condition)
>>{
>>  Console.WriteLine("Counter:"+Counter.ToString());
>>  Sleep(100);
>>  //do some processing
>>}
>>
>>
>>Hope this makes sense. It is a very simple case.
>>
>>Einar
>>
>>>Einar,
>>>
>>>Does your app join to all of the threads or just one/some of them? The debug code, where is it? Will it fire only after you've joined the thread or whether or not you join it? Remember, threads are not guaranteed to be processed in any particular order. A lot of factors go into determining when and how threads are processed. From the little bit of debugging output you have given and the little I know about your project (which is nothing), I could only speculate as to what is happening. My first guess would be that you are kicking off 4 threads, joining the third one (Counter:2) and it is finishing before the fourth thread. Obviously, that may not even be close, but I'd need more info to continue.
>>>
>>>HTH,
>>>Chad
>>>
>>>>I have a thread that I join using the overload Join method that accepts a timeout parameter and I see something like the following in the output window:
>>>>
>>>>The thread 'The Name of my thread' (0xd88) has exited with code 0 (0x0).
>>>>
>>>>
>>>>I added some debug/test code to the method that is running in the thread to print stuff to the console. Here is the code I added:
>>>>
>>>>Console.WriteLine("Counter:"+Counter.ToString());
>>>>
>>>>
>>>>Now the text in the output window will be something like this:
>>>>
>>>>Counter:0
>>>>Counter:1
>>>>Counter:2
>>>>The thread 'The Name of my thread' (0xd88) has exited with code 0 (0x0).
>>>>Counter:3
>>>>
>>>>
>>>>The code is running fine and everything is OK, but why does the line Counter:3 show up after the thread has exited?
>>>>
>>>>Any information on the subject is welcome.
>>>>
>>>>Thanks,
>>>>Einar
Semper ubi sub ubi.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform