0

I have multiple worker threads, in which each worker downloads an audio file. When the user closes the form in which these audio files get downloaded I want all of these worker threads to stop running.

I'm using a library that does the downloading of the audio files for me. So all I have to do in order to start downloading is audioDownloader.Execute();. This means I'm not using a while loop which I've seen used on msdb to end threads early.

I've tried aborting the threads on DownloadForm_FormClosing but when I try to reopen the download form, the audio files won't start downloading anymore. I've also tried using thread.Join() but the form just freezes... This is the code I use:

//DownloadForm.cs
private void DownloadForm_FormClosing(object sender, FormClosingEventArgs e)
{

    isdownloadformclosing = true;

    //each AudioFile holds a thread
    foreach(AudioFile v in AudioFiles)
    {
        v.thread.Abort();
        v.thread = null;
    }
}

//AudioFile.cs
try
{
    AudioDownloader.Execute();
}
catch(Exception e)
{
   if(!DownloadForm.isdownloadformclosing)
      DownloadForm.ShowErrorForId(this.Id, e);   
}
user1534664
  • 3,258
  • 8
  • 40
  • 66
  • What do you mean reopen the form? Does the AudioDownloader not support cancellation? Ideally, this component should've been designed to handle asynchronous and cancellable operations. – MicroVirus Jul 04 '14 at 12:47
  • @MicroVirus Yeah, apparently it hasn't been designed for that. By reopening the form I mean I have two forms, Form1 and a downloadform, when the download button on form1 gets pressed, the downloadform opens. In the downloadforms the worker threads start running and start downloading the audio files. That form can be reopened. – user1534664 Jul 04 '14 at 13:08
  • Possible duplicate of [How to stop BackgroundWorker on Form's Closing event?](http://stackoverflow.com/questions/1731384/how-to-stop-backgroundworker-on-forms-closing-event) – Hans Passant Jul 04 '14 at 13:12

2 Answers2

1

..when I try to reopen the download form, the audio files won't start downloading anymore

you can't restart the **Abort**ed thread

Once the thread terminates, it cannot be restarted with another call to Start.

Thread.Start Method

What you can do for your "reopen", for example:

protected Thread tDownloader;
...

if(tDownloader == null || tDownloader.ThreadState == ThreadState.Unstarted || tDownloader.ThreadState == ThreadState.Stopped)
{
    tDownloader = new System.Threading.Thread(() => { 
    ...TODO...
    });
    ....
    tDownloader.Start();
}

see ThreadState Enumeration for more detail

Thread.Abort - raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread

After call, ThreadState has Aborted status, but its state has not yet changed to Stopped.

Denis Kuzmin
  • 800
  • 7
  • 14
  • similar solution you can see in my source [this](https://bitbucket.org/3F/vssolutionbuildevent/src/e0a3a367d53ff8e72797506c8c9f9a42e1c34416/vsSolutionBuildEvent/OutputWPListener.cs?at=master#cl-150), _the full work sample_ – Denis Kuzmin Jul 04 '14 at 13:36
0

We can't really tell much without seeing the code for this AudioDownloader or AudioFile classes... either AudioDownloader should have a cancel method which (internally) deals with everything it needs to, or you need to check what's happening in AudioFile to cause the problem.

I suspect that if it's not expecting it's 'thread' object to be set to null, when you try to reuse the object it doesn't like it because it no longer has a thread... try recreating the AudioDownloader object each time your form loads - that should let it start as if it was starting from stratch again.

Like I say, without more code, not much we can do!

GoldieLocks
  • 845
  • 7
  • 22
  • I am using this library: https://github.com/flagbug/YoutubeExtractor. Here is my code http://pastebin.com/hw7bE0K3 – user1534664 Jul 04 '14 at 13:16