Hi Guys,
This is linked to a post yesterday about getting a form to respond when it doing other things and looses focus. If I create a seperate thread like
Thread StopButtonPressed =
new Thread(new ThreadStart (btnStop_Click(object sender, EventArgs e));
and later when I need this I call
StopButtonPressed.Start()
Will this just call the click routine or allow the button to be clicked, I'm pretty sure it will call the click.
I have done a bit reading and found out about the Background Worker Thread
which seems to take away some of the creation of the Tread but I am told it takes some "jiggery pokery" to get it to see a button click. Help!!!!

1 answers
All that will do is to execute the btn_Stop click method in a separate thread. It won't detect when the button is clicked which will always occur on the UI thread.
You could prevent the UI from freezing by running your CPU intensive task in the DoWork() method of a BackgroundWorker (see this tutorial for how to go about this).
However, if the application is working OK on a single threaded basis, then I'd try the Application.DoEvents() approach I mentioned in your previous question before you launch off and refactor the application to use a BackgroundWorker.
EDIT
To cancel the BackgroundWorker (not covered in the tutorial) proceed as follows:
answered one year ago by:
17279
329
Hi, Thanks for that. If I try to get the main chunk of code working in a thread (which incedently spawns another thread, tower of babel situation ahead!) is it likely to get treated differently by Windows, I'm not really fussed about interacting with other code as it should be run stand alone.
17279
If you use a BackgroundWorker, then the Windows thread scheduler will constantly switch between it and the main thread (unless the threads are running on different cores) to see if there are any UI events pending on the latter and service them if there are. So, if you press the Stop button, then it should execute fairly quickly. Incidentally, the tutorial didn't cover cancellation so I've edited my answer to show how it's done.
329
Hi, so if I get it, just too make sure I can run my main program as it is and just add the background worker to check the button been clicked. The only problem appears then to be wether the button has been clicked..... Glenn
17279
The reason why the button click isn't getting processed quickly enough right now is because everything is running on the same thread and what you're doing is so CPU-intensive that UI events take a back seat. Moving the CPU-intensive stuff to the DoWork() method of the BackgroundWorker should make the UI thread responsive again and so when you click the button to stop the process, it should react much more quickly than before.
329
The person that didn't want me to Application.DoEvents() has left my left shoulder so tried it, appears to work quicker than it did. Problem solved (or delayed until we get some fresh boards to test) Thanks Glenn