Comment intercepter l'exception dans le thread principal si l'exception se produit dans le thread secondaire?Comment intercepter l'exception dans le thread principal si l'exception se produit dans le thread secondaire?
L'extrait de code pour le scénario est donné ci-dessous:
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();
}
}
Vous ne pouvez pas. Pour un, au moment où l'exception est levée, le try ... catch dans le thread principal est déjà terminé. –