[英]C# event exception not caught in parent method
我正在努力实现缓存的get方法。 如果已超过最大等待时间(在我的情况下为100ms用于测试),则此方法将返回给调用方。
我的问题是,在计时器触发事件之后,永远不会到达异常。
请帮我理解为什么? ( 我读到事件是在同一线程上执行的,所以这不是问题 )
public static T Get<T>(string key, int? maxMilisecondsForResponse = null)
{
var result = default(T);
try
{
// Return default if time expired
if (maxMilisecondsForResponse.HasValue)
{
var timer = new System.Timers.Timer(maxMilisecondsForResponse.Value);
timer.Elapsed += OnTimerElapsed;
timer.AutoReset = false;
timer.Enabled = true; // start the timer
}
var externalCache = new CacheServiceClient(BindingName);
Thread.Sleep(3000); // just for testing
}
catch (Exception ex)
{
// why is the exception not caught here?
}
return result;
}
private static void OnTimerElapsed(object source, System.Timers.ElapsedEventArgs e)
{
throw new Exception("Timer elapsed");
}
从MSDN
Timer组件捕获并抑制事件处理程序为Elapsed事件抛出的所有异常。 在将来的.NET Framework版本中,此行为可能会更改。
并继续
但是请注意,对于异步执行并包括await运算符(在C#中)或Await运算符(在Visual Basic中)的事件处理程序,情况并非如此。 这些事件处理程序中引发的异常将传播回调用线程。
请看一下异常处理(任务并行库)
下面是一个应用示例:
public class Program
{
static void Main()
{
Console.WriteLine("Begin");
Get<string>("key", 1000);
Console.WriteLine("End");
}
public static T Get<T>(string key, int? maxMilisecondsForResponse = null)
{
var result = default(T);
try
{
var task = Task.Run(async () =>
{
await Task.Delay(maxMilisecondsForResponse.Value);
throw new Exception("Timer elapsed");
});
task.Wait();
}
catch (Exception ex)
{
// why the exception is not catched here?
Console.WriteLine(ex);
}
return result;
}
}
timer
正在自己的线程中执行,但是您无法在调用者级别捕获异常。 因此,在这种情况下使用计时器不是一个好的方法,您可以通过创建Task
操作来更改它。
var result = default(T);
CacheServiceClient externalCache;
if (!Task.Run(() =>
{
externalCache = new CacheServiceClient(BindingName);
return externalCache;
}).Wait(100))//Wait for the 100 ms to complete operation.
{
throw new Exception("Task is not completed !");
}
// Do something
return result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.