Link to home
Start Free TrialLog in
Avatar of rushtheweb
rushtheweb

asked on

BackgroundWorker w/Mutex - URGENT

Currently Im using the BackgroundWorker class in .NET 2.0 to access an FTP class and download a file; however because of the way threading works it all crashes since it isnt waiting for my BackgroundWorker to finish and because there might be more than one BackgroundWorker attempting to use the FTP class which doesnt support that, so the solution was to use Mutex to make each new BackgroundWorker wait until the one before it finishes. That fixes the problem with multiple BackgroundWorkers, however it doesnt fix the problem where the main app isnt waiting for all the BackgroundWorkers to finish working; is it possible to use Mutex to make it wait for the BackgroundWorkers?

This is all in an effort to make the main form stop being unresponsive during FTP uploading/downloading.
Avatar of rushtheweb
rushtheweb

ASKER

Here is some code snippets im using to hopefully help paint a better picture.

[The following code is in a for loop which is grabbing each of the selected items from a list box and starting a upload in the background worker thread, while trying to us MuteX to wait for that thread to finish.]

                        storage temp_s = new storage();
                        temp_s.is_directory = false;
                        temp_s.name = lstLocal.Items[index].SubItems[1].Text;
                        temp_s.parent_directory = this.LocalDirectory;

                        Mutex m = new Mutex(false, "FTPLock");
                        uploadworker.RunWorkerAsync(temp_s);
                        m.WaitOne();
                        m.ReleaseMutex();


[The following code is in the DoWork event for the called background worker; the code sets up a MuteX and waits for others to finish if they exist.]

               Mutex m = new Mutex(true, "FTPLock");
              
            m.WaitOne();

            storage temp = (storage)e.Argument;
            upload_file(temp.parent_directory, temp.name);
           
            m.ReleaseMutex();
can't you use IsBusy property of backgroundworker in formclosing?

something like this:

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                  if (backgroundWorker1.IsBusy)
                        e.Cancel = true;
            }

Heidar
No because the form doesnt close after they finish; the waiting and use of mutex are so the background workers dont try to upload files all at once and crash the app; additionally I need the main GUI to wait for all the background workers to finish before it continues because if it doesnt then it crashes due to it modifying the FTP class the background workers are trying to use to upload files.

Im hoping to somehow use backgroundworkers and mutex to fix my describes problems and to stop the main GUI from freezing during the uploading process.

Thanks :)
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial