How to catch exception in the main thread if the exception occurs in the secondary thread?

Posted by Ashish Ashu on Stack Overflow See other posts from Stack Overflow or by Ashish Ashu
Published on 2010-06-01T05:15:11Z Indexed on 2010/06/01 5:23 UTC
Read the original article Hit count: 323

Filed under:
|
|

How to catch exception in the main thread if the exception occurs in the secondary thread?

The code snippet for the scenario is given below:

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            Thread th1 = new Thread(new ThreadStart(Test));
            th1.Start();               
        }
        catch (Exception)
        { 

        }
    }

    void Test()
    {
        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(100);

            if (i == 2)
                throw new MyException();
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET