views:

83

answers:

1

Hi all, I have a MDI WinForms application that can perform several tasks. Each task is running as a backgroundWorker.

What is the good approach to control the running threads:

  • check whether the specific thread is running
  • stop specific thread ?

For example it shouldn't be possible to run the same task simultaneously.

May be I need a separate class where I can store isTaskRunning variable?

How is the good way to do this?

TIA

+1  A: 

One approach you could take would be to create a class per task. Then let that class perform the tasks. It will know if multiple tasks could be used at once, and restrict access to each of them. Hence :

public class LongUploadTask
{
  private bool isRunning;


  public void Execute()
  {
    if(!isRunning)
    {
       //etc...
    }
  }
}

Note, I have left out locking code if you are accessing this from multiple threads, which I don't think you are. In addition, if you wanted to execute this asyncronously, you could use asynchronous delegates and manual reset events to achieve what your BackgroundWorker's are doing.

Craig Wilson
this is how i would do it as well :)
Frederik Gheysels