后台线程中抛出的异常 [英] Exceptions Thrown in Background Threads

查看:87
本文介绍了后台线程中抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:


[开始代码]


void开始()

{

if(!TryToDoSomething())

ShowErrorMessage();

}


void TryToDoSomething()

{

尝试

{

线程主题=新主题(新主题启动(DoSomething);

thread.IsBackground = true;

thread.Start();


返回true;

}

catch(SomeException)

{

返回false;

}

}


无效DoSomething()

{

DoSomethingWhichMightThrowASomeException();

}

[结束代码]


尝试捕捉TryToDoSomething中的SomeException似乎无法工作,

大概是因为抛出了异常这个

是否正确?


如果是这样,最好处理这个异常是为了让我能够/>
调用ShowErrorMes当它被抛出时主线程中的sage?

Consider:

[begin code]

void Start()
{
if (!TryToDoSomething())
ShowErrorMessage();
}

void TryToDoSomething()
{
try
{
Thread thread = new Thread(new ThreadStart(DoSomething);
thread.IsBackground = true;
thread.Start();

return true;
}
catch (SomeException)
{
return false;
}
}

void DoSomething()
{
DoSomethingWhichMightThrowASomeException();
}

[end code]

Trying to catch the SomeException in TryToDoSomething doesn''t seem to work,
presumably because the exception is thrown in a separate thread. Is this
correct?

If so, what''s the best was of dealing with this exception so that I can
call ShowErrorMessage in the main thread when it''s thrown?

推荐答案

试图捕获TryToDoSomething中的SomeException并不是'似乎
工作,大概是因为异常是在一个单独的线程中引发的。这个
是否正确?


这是正确的。你必须捕获抛出异常的异常。

如果是这样,最好处理这个异常,以便我可以在主线程中调用ShowErrorMessage。抛出了?
Trying to catch the SomeException in TryToDoSomething doesn''t seem to work, presumably because the exception is thrown in a separate thread. Is this
correct?
That is correct. You must catch the exception on which it is thrown.

If so, what''s the best was of dealing with this exception so that I can
call ShowErrorMessage in the main thread when it''s thrown?




这是为了说明的目的而做到这一点的方法;我实际上不会这样做。$ / b

私有异常theException = null;

void MainEntryPointToTryToDoSomething()

{

线程线程=新线程(新的ThreadStart(DoSomething);

thread.IsBackground = true;

线程。 Start();

thread.Join(); //等到线程终止

if(theException!= null)

ShowErrorMessage(theException );

}


无效DoSomething()

{

尝试

{

DoSomethingWhichMightThrowAnException();

|}

catch(exception ex)

{

theException = ex; //保存它

}

}

基本上这个想法是保存异常对象所以它是可用的

到将显示错误消息的线程。这个例子使用了一个

全局变量,异常。更好的方法是使用

使用的异步模式b是BCL

另请注意,您必须使用thread.Join或其他一些同步的原始数据,如异步回调,才能在线程时正确地重新定位

已完成。



Here''s a way to do that for purposes of illustration; I would not actually
do it this way.

private Exception theException = null;
void MainEntryPointToTryToDoSomething()
{
Thread thread = new Thread(new ThreadStart(DoSomething);
thread.IsBackground = true;
thread.Start();
thread.Join(); // wait until thread terminates
if ( theException != null )
ShowErrorMessage(theException);
}

void DoSomething()
{
try
{
DoSomethingWhichMightThrowAnException();
|}
catch(Exception ex)
{
theException = ex; // save it
}
}
Basically the idea is the save the exception object so that it is available
to the thread which will display the error message. This example used a
global variable, theException. A better way to handle this is to use the
Async pattern as used by the BCL
Note also that you must use thread.Join, or some other synchronization
primitive such as an async callback, to correctly rendevous when the thread
is done.


David Levine写道:
David Levine wrote:
这是一种方法为了说明的目的;我实际上不会这样做。


我喜欢这个解决方案的简单性。你为什么不这样做呢?

这是一个坏主意吗?

处理这个的更好方法是使用
异步模式BCL使用
Here''s a way to do that for purposes of illustration; I would not actually
do it this way.
I like the simplicity of this solution. Why wouldn''t you do it this way --
is it a bad idea?
A better way to handle this is to use the
Async pattern as used by the BCL




嗯...我从来没有听说过这个。 Google也没有发现任何事情。


感谢您的帮助。



Hmm... I''ve never heard of this. Google didn''t turn up anything, either.

Thanks for the help.


Cool Guy写道:
Cool Guy wrote:
考虑:

[开始代码]

void开始()
{
if(!TryToDoSomething())
ShowErrorMessage();
}
void TryToDoSomething()
{
尝试
{
线程thread = new Thread(新的ThreadStart( DoSomething);
thread.IsBackground = true;
thread.Start();

返回true;
}
catch(SomeException)
{
返回false;
}

无效DoSomething()
{
DoSomethingWhichMightThrowASomeException();
}

[结束代码]

尝试捕获TryToDoSomething中的SomeException似乎无法工作,大概是因为异常被抛入一个单独的
thread。这是正确的吗?


是的。

如果是这样,什么是最好的处理这个异常,以便我可以在主线程中调用ShowErrorMessage时抛出它?
Consider:

[begin code]

void Start()
{
if (!TryToDoSomething())
ShowErrorMessage();
}

void TryToDoSomething()
{
try
{
Thread thread = new Thread(new ThreadStart(DoSomething);
thread.IsBackground = true;
thread.Start();

return true;
}
catch (SomeException)
{
return false;
}
}

void DoSomething()
{
DoSomethingWhichMightThrowASomeException();
}

[end code]

Trying to catch the SomeException in TryToDoSomething doesn''t seem to
work, presumably because the exception is thrown in a separate
thread. Is this correct?
Yes.
If so, what''s the best was of dealing with this exception so that I
can call ShowErrorMessage in the main thread when it''s thrown?




使用事件。假设您有一个类Main,它控制您的
应用程序的主要线程(例如Windows窗体)和类工作者。那个

在一个单独的线程上执行冗长的操作,以避免阻塞Main'的
线程。如果Worker公开了一个事件错误,只要需要将一个

异常传递给工人的所有者,就会触发该事件,Main可以简单地

订阅错误事件通过标准事件处理:


/ *请注意,ErrorEventArgs和ErrorEventHandler确实已存在于

System.IO * /


class Main {

私人工人;

public Main(){

this.worker = new worker ();

this.worker.Error + = new ErrorEventHandler(this.Worker_Error);

}


private void Worker_Error (对象发送者,ErrorEventArgs e){

// CAVEAT:如果这是一个WinForm类,请先调用

//回到UI线程!

//做点什么......显示信息,登录档案等等。

}

}


class Worker {

公共事件ErrorEventHandler错误;


public void Work(){

//启动DoW ork()in another thread

}


protected virtual void OnError(ErrorEventArgs e){

ErrorEventHandler handler = Error;

if(handler!= null){

handler(this,e);

}

}


私有无效DoWork(){

尝试{

//重物......

}

catch(Exception ex){

OnError(new ErrorEventArgs(ex));

}

} < br $>
}


当然这种方法可以将任何东西传回Main,

包括Work()的完成结果。另外,它很好地抽象了如何使用
Work()实际上使DoWork()在另一个线程上运行...是否使用它自己的托管线程,一个异步代理,无论如何 - 方法是相同的。


注意Worker_Error中的CAVEAT注释:如果你在Windows中使用这种方法

Forms应用程序,你*必须*保证所有这些回调通过确保每次调用

必要时使用Control.InvokeRequired

和Control.Invoke()编组回UI线程。


干杯,


-

Joerg Jooss
jo ********* @ gmx.net


这篇关于后台线程中抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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