繁体   English   中英

C#线程错误+ Backgroundworker组件

[英]C# Threading error + Backgroundworker component

我收到错误消息,当我尝试通过调度程序更新UI元素时,对象引用未设置为对象实例。

示例代码是->

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

// close the progressbar over here

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

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

您确定_progressBar的值不为null吗? 也许在另一个时间点它为null

我将添加以下行进行检查:

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