c# - Stopping work from one thread using another thread -


Not sure that my title has been given the word well, but whatever it is :)

< P> I have two threads: the main thread with the work needed to work, and a worker thread in which there is a form with a progress bar and a cancel button in normal code, this will be another way, but I In case it can not do

When the user clicks the cancel button, a prompt asks if he really wants to cancel the job. The problem is that work on the main thread continues. I can stop main thread work and get it this way, but when I click on "yes" shortly, I want to stop working on it.

Example:

  // The main thread works here t1 = new thread (new threadstart (progressForm_start)); T1.Start (); Working for // (i = 0; i & lt; = 10000; i ++) {semaphore.WaitOne (); If (pBar.Running) bgworker_ProgressChanged (i); Semaphore.Release (); Break if (pBar.Insancelled); } T1.Abort (); // The work of the main thread ends here // Start the progress bar in the form of zero in the second thread in Form_Start () {pBar.Status ("Start"); PBar.ShowDialog (); }  

I can include a prompts in the theoretically cancel option () function, but then I have to do it everywhere to implement that class.

I have a couple of quick comments:

  1. Avoid using Thread.Abort () .
  2. Create a background thread in your thread: thread.isbackground = true (it will automatically exit the thread when your app is released).

This is a detailed discussion on how to run a strong thread safe :

To prevent work from being on the main thread, Something must do:

  Boolean volatile hysin = true; Working for static zero major (...) {// ... // (i = 0; i  

Comments