【发布时间】:2011-10-30 05:06:44
【问题描述】:
我有一个相当复杂的程序,所以我不会把整个东西都扔在这里。这是一个简化的版本:
class Report {
private BackgroundWorker worker;
public Report(BackgroundWorker bgWorker, /* other variables, etc */) {
// other initializations, etc
worker = bgWorker;
}
private void SomeCalculations() {
// In this function, I'm doing things which may cause fatal errors.
// Example: I'm connecting to a database. If the connection fails,
// I need to quit and have my background worker report the error
}
}
// In the GUI WinForm app:
// using statements, etc.
using Report;
namespace ReportingService {
public partial class ReportingService : Form {
// My background worker
BackgroundWorker theWorker = new BackgroundWorker() {
WorkerReportsProgress = true
};
// The progress changed event
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
// e.UserState and e.ProgressPercentage on some labels, etc.
}
// The do work event for the worker, runs the number crunching algorithms in SomeCalculations();
void worker_DoWork(object sender, DoWorkEventArgs e) {
Report aReport = e.Argument as Report;
aReport.SomeCalculations();
}
// The completed event, where all my trouble is. I don't know how to retrieve the error,
// or where it originates from.
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
// How, exactly, do I get this error message? Who provides it? How?
if (e.Error != null) {
MessageBox.Show("Error: " + (e.Error as Exception).ToString());
}
else if (e.Cancelled) {
MessageBox.Show("Canceled");
}
// operation succeeded
else {
MessageBox.Show("Success");
}
}
// Initialization of the forml, etc
public ReportingService() {
InitializeComponent();
theWorker.ProgressChanged += worker_ProgressChanged;
theWorker.DoWork += worker_DoWork;
theWorker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
// A button that the user clicks to execute the number crunching algorithm
private void sumButton_Click(object sender, EventArgs e) {
Report myReport = new Report(theWorker, /* some other variables, etc */)
theWorker.RunWorkerAsync(myReport);
}
}
}
这是我的逻辑,如果我以错误的方式进行,请纠正我:
我将这个类从 GUI 中抽象出来,因为它有大约 2000 行,并且需要是它自己的自包含对象。
我将后台工作人员传给我的班级,以便我可以报告我的数字运算进度。
我不知道该怎么做是让后台工作人员知道我的班级发生了错误。为了将 RunWorkerCompleted 参数作为异常获取,我的 try/catch 块需要去哪里,在 catch 块中我应该做什么?
感谢您的帮助!
编辑:
我尝试了以下方法来测试错误处理:
请记住,我损坏了我的数据库连接字符串以故意接收错误消息。
在我的课堂上我这样做:
// My number crunching algorithm contained within my class calls a function which does this:
// try {
using (SqlConnection c = GetConnection()) { // note: I've corrupted the connection string on purpose
c.Open(); // I get the exception thrown here
using (SqlCommand queryCommand = new SqlCommand(query, c)) { /* Loop over query, etc. */ }
c.Close();
}
// } catch (Exception e) { }
1。
据我了解,未处理的异常被强制转换为RunWorkerCompletedEventArgs 的Error 部分?当我尝试这个时,我得到以下信息:
// In my winform application I initialize my background worker with these events:
void gapBW_DoWork(object sender, DoWorkEventArgs e) {
Report aReport = e.Argument as Report;
Report.Initialize(); // takes ~1 minute, throws SQL exception
Report.GenerateData(); // takes around ~2 minutes, throws file IO exceptions
}
void gapBW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (e.Error != null) { // I can't get this to trigger, How does this error get set?
MessageBox.Show("Error: " + (e.Error as Exception).ToString());
}
else if (e.Cancelled) {
MessageBox.Show("Canceled: " + (e.Result).ToString());
}
else {
MessageBox.Show("Success");
}
}
Visual Studio 说我的应用程序在 c.Open() 上因未处理的异常而失败。
2。 当我在我的 DoWork 函数中放置一个 try/catch 块时:
void gapBW_DoWork(object sender, DoWorkEventArgs e) {
try {
Report aReport = e.Argument as Report;
aReport.Initialize(); // throws SQL exceptions
aReport.GenerateData(); // throws IO file exceptions
}
catch (Exception except) {
e.Cancel = true;
e.Result = except.Message.ToString();
}
}
void gapBW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (e.Error != null) { // I can't get this to trigger, How does this error get set?
MessageBox.Show("Error: " + (e.Error as Exception).ToString());
}
else if (e.Cancelled) {
MessageBox.Show("Canceled: " + (e.Result).ToString());
}
else {
MessageBox.Show("Success");
}
}
我得到一个 TargetInvocationException 在 Program.cs 中自动生成的 Application.Run(new ReportingService()); 行中未处理。我在 RunWorkerCompleted 上放置了一个断点,可以看到 e.Cancelled = true、e.Error = null 和 e.UserState = null。 e.Cancelled 中包含的消息只是“操作已取消”。我想我从 e.Result 的无效演员表中收到TargetInvocationException(因为它是空的)。不过,我想知道的是,为什么 e.Error 仍然为 null 并且 e.Canceled 不包含有关 为什么 操作被取消的任何有用信息?
3。
当我尝试在 DoWork 中设置 e.Canceled = true; 异常捕获时,我设法在我的 RunWorkerCompleted 函数中触发了 else if (e.Cancelled) { 行。我认为这是为请求取消作业的用户保留的吗?我是否从根本上误解了后台工作人员的功能?
【问题讨论】:
标签: c# multithreading exception backgroundworker