如何处理从一个BackgroundWorker线程异常? [英] How to handle exceptions from a BackgroundWorker thread?

查看:255
本文介绍了如何处理从一个BackgroundWorker线程异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WPF应用程序我有一个sheduled数据库访问的任务,定期通过一个定时器,这个任务在一个BackgroundWorker线程已经执行运行。

In a WPF app I have a sheduled database access task, periodically run by a timer and this task have been executed in a BackgroundWorker thread.

当连接尝试失败,我提出通过 try_catch 异常建设,我想更新UI线程的状态栏的文本。

When connection attempt failed I raise an exception by try_catch construction and I want to update a Status Bar text in a UI thread.

有一些prebuild活动建设的BackgroundWorker 执行本,像 DoWorkEventHandler RunWorkerCompletedEventHandler ,可用于本?如果没有,如何做的更好?

Is there some prebuild event construction in a BackgroundWorker for implementing this, something like DoWorkEventHandler or RunWorkerCompletedEventHandler, which can be used for this? If not, how to do it better?

编辑(添加):

如果我要处理异常的内部 RunWorkerCompletedEventHandler ,使用 e.Error 参数,这是行不通的。如果我离开异常未处理的的BackgroundWorker 线程,应用程序挂起和调试点至code中的字符串,是excuted内 BackgroundWorker的话题,说: 异常是未处理由用户code

If I want to handle the exception inside RunWorkerCompletedEventHandler, using e.Error parameter, it doesn't work. In case I leave exception unhandled in the BackgroundWorker thread, application hangs on and debugger points to the string of code which is excuted inside BackgroundWorker thread, saying that: Exception was unhandled by user code.

所以,在这种情况下,线程并不仅仅停留,信令 RunWorkerCompletedEventHandler ,它与错误而停止,但整个应用程序停止工作。

So, in this case, thread doesn't just stop, signalling to RunWorkerCompletedEventHandler that it stopped with error, but the whole application stop working.

推荐答案

一个WPF UI可以从后台线程使用Dispatcher.BeginInvoke进行更新。

A WPF UI can be updated from a background thread by using Dispatcher.BeginInvoke.

例如,如果你的背景code是一个窗口的一部分,那么你可以更新一个TextBlock:

For example if your background code was part of a Window then you could update a TextBlock:

this.Dispatcher.BeginInvoke((Action)(() =>
    {
        textBlock.Text = "Connection Failed!";
    }));

编辑:

如果你的背景code组中的一类比你的窗口等,你可以做一个界面的帮助:

If your background code were in a class other than your Window you could make an interface to help:

public interface IShowStatus
{
    void ShowStatus(string message);
}

实现该接口在窗口

Implement the interface in your Window

public void ShowStatus(string message)
{
   this.Dispatcher.BeginInvoke((Action)(() =>
       {
           textBlock.Text = message;
       }));
}

在你的类与背景工人做一个属性来保存引用的接口。

In your class with the background worker make a property to hold a reference to the interface.

public IShowStatus StatusDisplay { get; set; }

在你的窗口类的初始化背景类。

In your Window class initialize the background class.

public void InitBackground()
{
    BackgroundClass background = new BackgroundClass();
    background.StatusDisplay = this;
    ...

最后,在你的后台线程,你可以说:

Finally in your background thread you can say:

StatusDisplay.ShowStatus("Connection Failed!");

这篇关于如何处理从一个BackgroundWorker线程异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆