Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
}
}
Posted

1 solution

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:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900