I am trying to implement a Backgroundworker . This worker will call one of my methods that do lots of file processing as well as db processing. Name of the method is LoadASpreadSheet(), which is getting called from background worker DoWoker event. However it is not returning the catch exceptions. At present this method is void but I can return a string or anything else. Basically I need to assign the return value from LoadASpreadSheet() to e.Result at bkg_Dowork event right?
So question is how do I implement this into LoadASpreadSheet()
I need to return the catch Exception from this method ...please help me.
catch (Exception eX)
eX.Result ; ??
eX.GetType ; ??
Here is my class in short
public partial class MyClass1 : System.Windows.window
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.WorkerSupportsCancellation = true;
if (bgw != null && bgw.IsBusy && bgw.WorkerSupportsCancellation && !bgw.CancellationPending)
void bgw_DoWork(object sender, DoWorkEventArgs e)
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
if (e.Error != null)
msgErr = String.Concat(msgErr, e.Error.Message);
msgErr = String.Concat(msgErr, e.Error.Source);
errorBlock.Text = msgErr;
else if (e.Cancelled)
//LoadASpreadSheet() processing was cancelled
//Do whatever is needed after LoadASpreadSheet() has completed.
LoadASpreadSheet() method is implemented in the above class.
I have start pb and on click event I call StartBackgroundWorker();
I do have a cancel pb and on cancel click event it call CancelBackgroundWorker() so user can stop current thread or process.
Right now is not working correctly.
Therefore two main issues First: how do I return the exception from
LoadASpreadSheet() method into DoWorker event?
LoadASpreadSheet() called two other method that I mentioned you before and one of this method does talk to database to do delete /insert operation via stored proc etc. Delete/Insert takes place in a transaction and in case of error this method catch it too. However I don't think that should be any issues.
Second: Why CancelBackgroundWorker() is not working?
Please get back to me I need to implement this today.