简体   繁体   中英

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.

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();
                     }
                  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM