Sandepku
Sandepku

Reputation: 861

C# Threading error + Backgroundworker component

I am getting an error which says object reference not set to instance of an object when I try to update UI element via dispatcher.

The sample code is ->

backgroundworker.DoWork += >
{
// do some work here.

// close the progressbar over here

    _progressBar.Dispatcher.Invoke(DispatcherPriority.Normal, 
                                            new Action( _progressBar.Close);
}

I get an error Object reference not set in the _progressBar.Dispatcher.Invoke statement and my application totally hangs.

Upvotes: 1

Views: 738

Answers (1)

RoelF
RoelF

Reputation: 7573

Are you sure the value of _progressBar is not null? Maybe it is null at a different point in time.

I would add following lines to check for it:

new Action(() => {
                    if (_progressBar == null){
                        if (Debugger.IsAttached){
                            Debugger.Break();
                        } else {
                            Debug.Fail("_progressbar is null!");
                        }
                     } else {
                       _progressBar.Close();
                     }
                  });

Upvotes: 1

Related Questions