WPF: How to handle errors with a BackgroundWorker

血红的双手。 提交于 2019-12-21 06:15:29

问题


I am a bit of a newbie when it comes to windows client programming. I have a background worker that has a DoWork event and a RunCompleted event wired up. If an exception gets thrown in DoWork, I want to make changes to my UI, however, I cant because it is in a different thread. I can communicate the error to RunCompleted, but that doesn't help me either.


回答1:


call Dispatcher.BeginInvoke. Basically, you want code like this:

void UpdateState(WhatEverType someObject)
{
    if (! Dispatcher.CheckAccess())
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=>UpdateState(someObject));
    }
    else
    {
        //make the UI changes here.
    }
}


来源:https://stackoverflow.com/questions/532740/wpf-how-to-handle-errors-with-a-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!