我试图从故意失败的 Task.Run() 操作中捕获 AggregateException,但是没有抛出 AggregateException。为什么?
public void EnterWaitingRoom(string val)
{
try
{
Task.Run(() => InvokeHubMethod("HubMethod",
new object[] { val }));
}
catch (AggregateException ex)
{
// This exception is not caught
throw ex;
}
}
private async Task<object> InvokeHubMethod(string method, object[] args)
{
return await Task.Run(() => _hubProxy.Invoke<object>(method,
args).Result);
}
我希望抛出异常,但事实并非如此。我也尝试添加 .Wait 并且仍然没有得到异常。请求来自 Windows UI。有任何想法吗。谢谢。
这是您async/await
从 EventHandler输入的方式
public async void Button_Click(object sender, RoutedEventArgs args )
{
object result = await EnterWaitingRoom( "whatever" );
}
private Task<object> EnterWaitingRoom(string val)
{
return InvokeHubMethod(
"HubMethod",
new object[] { val } );
}
private Task<object> InvokeHubMethod(string method, object[] args)
{
return _hubProxy.Invoke<object>(
method,
args);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句