Here is the situation. I have a thread that gets started with the click of a button. It then works for approx 2 min (give or take quite a bit) and then finishes, updating the UI and all along the way. It is quite nice. Now I have decided to add a “Cancel” button to the mix. My question would be what is the best approach to stop a thread from the outside? What methods are best to avoid completely? What resources can I look into myself to get some more knowledge on the topic.
One other piece of info. The program was not written by me, I just “inherited” it, and am now trying to expand it’s functionality in small ways to make it more user friendly.
EDIT: Working with VS2008 and .NET 3.5
In fact if you want to safely cancel some background operation the best approach is to create private boolean field _isCancelled which will be shared between GUI thread and background thread. If user presses cancel you just set _isCancelled to false. And in the background thread regulary check if _isCancelled is turned to true and return the method.