【发布时间】:2019-12-06 12:03:28
【问题描述】:
我怎样才能捕捉到TimeoutException?
我想在 3 秒后赶上 TimeoutException。
但 3 秒后它会打印出 TimeoutException 而 It's too long. Timeout! 是预期的。
console application 无法捕获 TimeoutException。
public static void work()
{
Thread.Sleep(3000);
Console.WriteLine("TimeoutException");
throw new TimeoutException();
}
public static void Main(string[] args)
{
try
{
ThreadStart th = new ThreadStart(work);
Thread t = new Thread(th);
t.Start();
//Execute SearchProgram
t.Abort();
}
catch (ThreadInterruptedException)
{
Console.WriteLine("It's too long. Timeout!");
}
Console.WriteLine("Result : ~~~");
}
【问题讨论】:
-
你在抓
ThreadInterruptedException而不是TimeoutException -
您将
TimeoutException();扔到您的work方法中并期望捕获ThreadInterruptedException?
标签: c# multithreading timeoutexception