Link to home
Start Free TrialLog in
Avatar of jherington
jherington

asked on

BackgroundWorker.RunWorkerCompleted Not Firing

I am hitting an error when the 'workerGetFileList.RunWorkerAsync();' initially runs. I would expect the 'workerGetFileList_RunWorkerCompleted' to be successfully called after hitting the error (or upon any termination/completion of the DoWork) but it doesn't in the case of an error. I subsequently put a Try Catch in the 'workerGetFileList_DoWork' which seems to allow the 'workerGetFileList_DoWork' to complete successfully and then the 'workerGetFileList_RunWorkerCompleted' is called.

My understanding is that if any error occurs in the 'workerGetFileList_DoWork' then the 'workerGetFileList_RunWorkerCompleted' is called and the 'e.Error' can then be inspected and handled.

I am running VS2005 on XP.
All Relevent Code:
 
public Form1()
        {
            InitializeComponent();
 
            // I moved these from the designer to here
            // Not sure how they originally got in the designer...maybe a vs2008 feature?
            this.workerGetFileList.DoWork += new System.ComponentModel.DoWorkEventHandler(this.workerGetFileList_DoWork);
            this.workerGetFileList.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.workerGetFileList_RunWorkerCompleted);
 
            this.workerFileHash.DoWork += new System.ComponentModel.DoWorkEventHandler(this.workerFileHash_DoWork);
            this.workerFileHash.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.workerFileHash_RunWorkerCompleted);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            WebService = new MTOM_Library.CoreTransferWS.MTOMWse();
 
            // get the list of files to download from the server
            workerGetFileList.RunWorkerAsync();
 
            // set the default save folder
            this.txtSaveFolder.Text = Application.StartupPath;
 
            // configure the 'TaskPanel' which is used to dynamically show a progress bar + status message for each file transfer operation
            this.taskPanel1.RemoveItemsWhenFinished = true;
            this.taskPanel1.RemoveItemsOnError = false;
            this.taskPanel1.AutoSizeForm = false;
 
            // init the ThreadPool MaxThread size to the value in the control
            this.dudMaxThreads_ValueChanged(sender, e);
        }
 
private void workerGetFileList_DoWork(object sender, DoWorkEventArgs e)
        {
            // fetch the list of filenames from the web service
            try
            {
                string[] files = WebService.GetFilesList();
                // set the result with the return value (available to workerGetFileList_RunWorkerCompleted method as e.Result)
                e.Result = files;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Object moved to") || ex.Message.Contains("ReturnUrl=") || ex.Message.Contains("forcibly closed"))
                {
                    bAuthFailed = true;
                }
                return;
            }
        }
 
private void workerGetFileList_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (e.Error.Message.Contains("ReturnUrl=") || e.Error.Message.Contains("forcibly closed"))
                {
                    this.lblErrors.Text = "Forms Authentication Required";
                    this.chkLoginRequired.Checked = true;
                    this.txtUsername.Focus();
                    return;
                }
                else
                    this.lblErrors.Text = "Could not list files on server: " + e.Error.Message;
                return;
            }
            this.lblErrors.Text = "";
            this.lstDownloadFiles.Items.Clear();
            if (bAuthFailed == true)
            {
                this.lblErrors.Text = "Forms Authentication Required";
                this.chkLoginRequired.Checked = true;
                this.txtUsername.Focus();
                bAuthFailed = false;
                return;
            }
            else if(e.Result != null)
            {
                // copy the list of filenames into the listbox
                foreach (string s in e.Result as string[])
                    this.lstDownloadFiles.Items.Add(s);
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of jherington
jherington

ASKER

You were exactly correct, the code runs fine from the exe.
The code I was testing came from VS2008...and I don't believe they are getting the same problem. So I will keep my Try Catch in the DoWork so that I can test this code in VS2005.
Not a problem, glad I was able to help. ;=)