9

In a WPF app I have a sheduled database access task, periodically run by a timer and this task have been executed in a BackgroundWorker thread.

When connection attempt failed I raise an exception by try_catch construction and I want to update a Status Bar text in a UI thread.

Is there some prebuild event construction in a BackgroundWorker for implementing this, something like DoWorkEventHandler or RunWorkerCompletedEventHandler, which can be used for this? If not, how to do it better?

Edited (added):

If I want to handle the exception inside RunWorkerCompletedEventHandler, using e.Error parameter, it doesn't work. In case I leave exception unhandled in the BackgroundWorker thread, application hangs on and debugger points to the string of code which is excuted inside BackgroundWorker thread, saying that: Exception was unhandled by user code.

So, in this case, thread doesn't just stop, signalling to RunWorkerCompletedEventHandler that it stopped with error, but the whole application stop working.

rem
  • 16,745
  • 37
  • 112
  • 180
  • Thanks to all for answering! Taking into account some answers, I updated my question with more details. – rem Feb 20 '10 at 18:22

5 Answers5

6

The RunWorkerCompletedEventArgs e of the RunWorkerCompletedEventHandler contains property Error it is of type Exception. If no exception occurred during the work of the background thread the prpery has null as value. Else it contains the error that occurred.

IordanTanev
  • 6,130
  • 5
  • 40
  • 49
5

A WPF UI can be updated from a background thread by using Dispatcher.BeginInvoke.

For example if your background code was part of a Window then you could update a TextBlock:

this.Dispatcher.BeginInvoke((Action)(() =>
    {
        textBlock.Text = "Connection Failed!";
    }));

Edit:

If your background code were in a class other than your Window you could make an interface to help:

public interface IShowStatus
{
    void ShowStatus(string message);
}

Implement the interface in your Window

public void ShowStatus(string message)
{
   this.Dispatcher.BeginInvoke((Action)(() =>
       {
           textBlock.Text = message;
       }));
}

In your class with the background worker make a property to hold a reference to the interface.

public IShowStatus StatusDisplay { get; set; }

In your Window class initialize the background class.

public void InitBackground()
{
    BackgroundClass background = new BackgroundClass();
    background.StatusDisplay = this;
    ...

Finally in your background thread you can say:

StatusDisplay.ShowStatus("Connection Failed!");
Doug Ferguson
  • 2,538
  • 2
  • 16
  • 23
  • In my case the background code is not part of a Window but it runs inside a separate class. Moreover, exception takes place in a static method. So, THIS keyword is not valid in this case. PLease, help me understand, how I could reference my main UI window in this case? – rem Feb 20 '10 at 18:14
  • I added notes in reply to your question. – Doug Ferguson Feb 21 '10 at 13:39
3
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     Exception exceptionThrowDuringDoWorkEventHandler = e.Error;
}
Ken
  • 1,830
  • 3
  • 20
  • 32
  • When I try to throw an exception from the BackgroundWorker thread, the app just crashes with: Exception was unhandled by user code. I never get to bw_RunWorkerCompleted. Am I missing something? – Doug Oct 08 '14 at 03:37
3

Set the WorkerReportsProgress property of the background worker to true, then add an event handler for the event ProgressChanged. In the following code, I added an event handler for Form.Load also.

Now try the following code:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            MessageBox.Show(e.UserState.ToString());
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                //some code that throws an exception
                throw new NotImplementedException();
            }
            catch (Exception ex) 
            {
                backgroundWorker1.ReportProgress(0/*percent of progress*/, ex);

            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
Sameh Deabes
  • 2,960
  • 25
  • 30
0

If I want to handle the exception inside RunWorkerCompletedEventHandler, using e.Error parameter, it doesn't work. In case I leave exception unhandled in the BackgroundWorker thread, application hangs on and debugger points to the string of code which is excuted inside BackgroundWorker thread, saying that: Exception was unhandled by user code.

So, in this case, thread doesn't just stop, signalling to RunWorkerCompletedEventHandler that it stopped with error, but the whole application stop working.

I'm very curious about this. Try running the following code in a console app and see if your program crashes or if the error shows up in the RunWorkerCompletedEventArgs.

    static void Main(string[] args)
    {
        var bw = new BackgroundWorker();
        bw.DoWork += Bw_DoWork;
        bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
        bw.RunWorkerAsync();

        Console.ReadKey();
    }

    private static void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("Complete.");
        Console.WriteLine(e.Error);
    }

    private static void Bw_DoWork(object sender, DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    }
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66