如何从线程中获取异常消息...? [英] how to get an exception message from thread...???

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

问题描述

在我的线程中运行的方法给出了异常.我想将该异常消息发送到主线程.
请告诉我该怎么做.
这是我的代码部分

公共无效导入(字符串ID)
{
试试
{
线程t1 =新线程(ImportThread1);
线程t2 =新线程(ImportThread2);
t1.Start(obj1);
t2.Start(obj2);
t1.Join();
t2.Join();
}
catch(ex ex例外)
{
Console.WriteLine(ex.Message);
}
}

私有void ImportThread1(对象信息)
{
试试
{
Method1(info.ToString);
}
catch(ex ex例外)
{
扔前//我想将此异常传递给主线程
}
}

私有void ImportThread1(对象信息)
{
试试
{
Method2(info.ToString);
}
catch(ex ex例外)
{
扔前//我想将此异常传递给主线程
}
}

the method which runs in my thread gives an exception. i want to get that exception message to the main thread.
please tell me how to do this.
here is my code part

public void Import(string id)
{
try
{
Thread t1 = new Thread(ImportThread1);
Thread t2 = new Thread(ImportThread2);
t1.Start(obj1);
t2.Start(obj2);
t1.Join();
t2.Join();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

private void ImportThread1(object info)
{
try
{
Method1(info.ToString);
}
catch (Exception ex)
{
throw ex; // i want to pass this exception to the main thread
}
}

private void ImportThread1(object info)
{
try
{
Method2(info.ToString);
}
catch (Exception ex)
{
throw ex; // i want to pass this exception to the main thread
}
}

推荐答案

有很多解决方案可以做到这一点.

您可以使用AppDomain.CurrentDomain.UnhandledException来执行此操作.看看这个:


http://social.msdn.microsoft.com/论坛/en-US/csharpgeneral/thread/6a9d9809-551a-4dc8-9523-44eeb0b2dfdc [
There are lots of solutions to do this.

You might use AppDomain.CurrentDomain.UnhandledException to do this. Take a look into this :


http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6a9d9809-551a-4dc8-9523-44eeb0b2dfdc[^]

Other than that, you can also create a callback to send exception as data after handling it properly inside the child thread and ReThrow the exception in the main thread from the callback.

:rose:


这篇关于如何从线程中获取异常消息...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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