#284 Exceptions on created threads

open
nobody
None
5
2012-09-15
2012-05-01
Lior Tal
No

When using a method like ClickNoWait, a new thread is being created and code is executed on it.

In case of an exception on the other thread, the entire application crashes since it is not handled in the new thread.

Discussion

  • Lior Tal
    Lior Tal
    2012-05-01

    A proposed fix (that i am currently using for this):

    public static void AsyncActionOnBrowser(ThreadStart action, bool catchExceptions)
    {
    var thread = new Thread(() =>
    {
    try
    {
    action();
    }
    catch (Exception e)
    {
    Logger.LogDebug("An exception has occured during AsyncActionOnBrowser(): {0}", e);

                        if (!catchExceptions)
                        {
                            throw;
                        }
                    }
                });
    
            thread.SetApartmentState(ApartmentState.STA);
    
            thread.Start();
            thread.Join(500);
        }