简体   繁体   English

未在Task.Run()中引发AggregateException

[英]AggregateException Not Thrown in Task.Run()

I am trying to catch an AggregateException from a Task.Run() operation which intentionally fails, however an AggregateException is not thrown. 我正在尝试从故意失败的Task.Run()操作中捕获AggregateException,但是不会引发AggregateException。 Why? 为什么?

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);
}

I expect the exception to be thrown but it is not. 我希望会抛出异常,但事实并非如此。 I also tried adding .Wait and still don't get the exception. 我也尝试添加.Wait,但仍然没有收到异常。 The request is coming from a windows UI. 该请求来自Windows UI。 Any ideas. 有任何想法吗。 Thanks. 谢谢。

This is how you enter the async/await from an EventHandler 这是您从EventHandler输入async/await的方式

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM