Come rilevare l'eccezione nel thread principale se l'eccezione si verifica nel thread secondario?Come rilevare l'eccezione nel thread principale se l'eccezione si verifica nel thread secondario?
Il frammento di codice per lo scenario è il seguente:
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();
}
}
Non è possibile. Per uno, nel momento in cui viene lanciata l'eccezione, il try ... catch nel thread principale è già stato completato. –