Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Easy Threading Question

Started by Geronimo2000 Mar 17, 2005 at 6:38 PM 8 replies 2k views
Original Post
Geronimo2000
Geronimo2000
I have a lame threading model in my program and wish to improve it. Let's say I am in the Dispose() method of the main Form in my program. At this point I wish terminate every thread that was spawned inside of my application. I assume there is a way to do this, and tried some of the threading functions a while back, but I couldn't quite get it to work. What function(s) would you recommend?
turnpast
turnpast
The best way to stop a thread is for it to complete running normaly. That is to reach the end of its code block. To be useful many threads run in a loop. Make the loop dependant on a flag and set that flag to false when you want the thread to stop.

volatile bool running;Thread t = new Thread(delegate void {  while(running){    //...  }});void StopThread(){runing = false;}


Use join if you want to wait for a thread to terminate regularly.

If you cannot make your threads terminate regularly you can use the Abort() method, but you will have to trap a ThreadAbortException in the thread and try to cleanup correctly when it is thrown. You should not need to do this unless your thread blocks on something you cannot control (some types of IO for example). Generaly try to get all your threads to terminate regularly.
Geronimo2000
Geronimo2000
OK, thanks - is there one function I can call that will basically call Thread.Abort() on all the threads I have spawned in my application. This would be very helpful in some situations by allowing me to spawn a thread and not keep it in a global list.
ernow
ernow
A side-note: if you mark all your spawned threads as BackgroundThreads this way they will terminate as soon as there are only backgroundthreads left.

Cheers
thona
thona
Quote:
Original post by Geronimo2000
OK, thanks - is there one function I can call that will basically call Thread.Abort() on all the threads I have spawned in my application. This would be very helpful in some situations by allowing me to spawn a thread and not keep it in a global list.


No, it would not. It would seem helpfull UNTIL you delete the one thread you were not aware of (that was maybe started by a component) and fuck up your program and spend months figuring out what happens.

You assume, falsely, that all threads are from you.
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
Geronimo2000
Geronimo2000
Thanks for pointing me in the right direction. I have solved my problem by setting the IsBackground property of the threads I am spawning to true.
ernow
ernow
Becarefull with that because they get terminated/killed that way; you are not able to nicely shutdown these threads!

Cheers
Geronimo2000
Geronimo2000
Quote:
Becarefull with that because they get terminated/killed that way; you are not able to nicely shutdown these threads!


You had me worried, so I went back and checked this out. After I set the IsBackground property to true I put a breakpoint in my code where the threads catch the ThreadAbortException. It was getting triggered when I exited the application as you would expect. But, it was also getting triggered when I manually called Abort() on the thread.

Maybe I misunderstood your warning, but it looks like the IsBackground property allows you to have the best of both worlds should you want it.
ernow
ernow
:) My suggestion was even better than I thought! Thank for the research.

Cheers

[Edited by - ernow on March 20, 2005 2:20:06 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.