We help IT Professionals succeed at work.

BackgroundWorker.RunWorkerCompleted Not Firing

jherington
jherington asked
on
2,369 Views
Last Modified: 2013-12-16
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

Comment
Watch Question

Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem, glad I was able to help. ;=)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.