简体   繁体   English

C#线程错误+ Backgroundworker组件

[英]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. 我收到错误消息,当我尝试通过调度程序更新UI元素时,对象引用未设置为对象实例。

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. 我收到未在_progressBar.Dispatcher.Invoke语句中设置的错误对象引用,并且我的应用程序完全挂起。

Are you sure the value of _progressBar is not null? 您确定_progressBar的值不为null吗? Maybe it is null at a different point in time. 也许在另一个时间点它为null

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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