C# ThreadPool QueueUserWorkItem Exception Handling

12,336

Solution 1

The best practice is that your background threads should not throw exceptions. Let them handle their exceptions on their own.

Ideally you should wrap the code in your method that executes on a thread in a try-catch block and handle the exception in the catch block. Do not re-throw it from the catch block.

Read this for more details. http://www.albahari.com/threading/#_Exception_Handling

If you want to update the UI from background thread you can do that by using Control.InvokeRequired property and Control.Invoke method. See the MSDN links for details and examples.

Solution 2

It's not possible to transfer exception from a thread to another one. What can you do is to built some synchronization mechanism to transfer exception information between threads and then throw a new exception from the target thread something like:

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
        {
            // do something that can throw an exception
        }
        catch (Exception ex)
        {
            _savedException = ex;
            _exceptionEvent.Set();
        }
    }
}

If you have a Win form application things are much simpler. In the catch clause of your thread use Invoke (or BeginInvoke) method of your form, providing it with the exception details. In the method launched with Invoke you can rethrow or treat your exception as you want.

Share:
12,336
Aks
Author by

Aks

Updated on June 20, 2022

Comments

  • Aks
    Aks almost 2 years

    Whenever a thread in my ThreadPool throws an exception, my code seems to be stuck at the catch block inside the thread function. How do I get the exception back to the main thread?

  • Aks
    Aks about 13 years
    But my UI has to display the exception. What can I do?
  • David Mårtensson
    David Mårtensson about 13 years
    Catch it in the thread and use a queue (pipe) to send a message back to the UI thread to display the error. Exceptions over thread borders is a bad thing and very hard to trouble shot.