Unable to pass exception from thread to another. What you can do is create a synchronization mechanism to transfer information about exceptions between threads, and then output a new exception from the target thread something like this:
class Program { Exception _savedException = null; AutoResetEvent _exceptionEvent = new AutoResetEvent(false); static void Main(string[] args) { Program program = new Program(); program.RunMain(); } void RunMain() { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod)); while (true) { _exceptionEvent.WaitOne(); if (_savedException != null) { throw _savedException; } } } void ThreadMethod(object contxt) { try {
If you have a Win Form application, things are much simpler. In the catch clause of your stream, use the Invoke (or BeginInvoke) method of your form, providing it with exception information. In a method launched with Invoke, you can restore or handle your exception as you wish.
Eugene
source share