如何安全地调用异步方法在C#没有等待 [英] How to safely call an async method in C# without await

查看:118
本文介绍了如何安全地调用异步方法在C#没有等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 async 方法,不返回数据:

 公共异步任务MyAsyncMethod()
{
//做一些东西异步,不返回任何数据
}

我从另一种返回一些数据的方法中调用这个方法:

  public string GetStringData()
{
MyAsyncMethod(); //这会产生一个警告并吞下异常
returnhello world;
}

调用 MyAsyncMethod()而不等待它会导致因为此呼叫未被等待,因此当前方法继续在电话完成之前运行在视觉工作室中的警告。在该警告的页面上,它指出:


只有当您确定不想要等待异步调用完成,并且被调用的方法将不会引发任何异常


I 我确信我不想等电话来完成;我不需要或有时间。 但是呼叫可能引发异常。



我偶然遇到这个问题了几次,



如何在不等待结果的情况下安全地调用异步方法?



更新:



对于建议我等待结果的人,这是响应Web请求的代码我们的Web服务(ASP.NET Web API)。在UI上下文中等待UI线程空闲,但在Web请求调用中等待等待任务完成,然后才能响应该请求,从而无需增加响应时间。

解决方案

如果你想异步获取异常,你可以这样做:

  MyAsyncMethod()。 
ContinueWith(t => Console.WriteLine(t.Exception),
TaskContinuationOptions.OnlyOnFaulted);

这将允许您处理除主线程之外​​的线程上的异常。这意味着您不必等待调用 MyAsyncMethod 的线程调用 MyAsyncMethod() ;但是,仍然允许你做一些例外的事情 - 但是只有在发生异常的情况下才可以。



更新:



<在技​​术上,你可以做一些类似于 await 的类似的东西:

  try 
{
等待MyAsyncMethod()。ConfigureAwait(false);
}
catch(Exception ex)
{
Trace.WriteLine(ex);
}

...如果您需要具体使用尝试 / catch (或使用),但是我发现 ContinueWith 有点更明确,因为你必须知道什么 ConfigureAwait(false) 表示。


I have an async method which returns no data:

public async Task MyAsyncMethod()
{
    // do some stuff async, don't return any data
}

I'm calling this from another method which returns some data:

public string GetStringData()
{
    MyAsyncMethod(); // this generates a warning and swallows exceptions
    return "hello world";
}

Calling MyAsyncMethod() without awaiting it causes a "Because this call is not awaited, the current method continues to run before the call is completed" warning in visual studio. On the page for that warning it states:

You should consider suppressing the warning only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions.

I'm sure I don't want to wait for the call to complete; I don't need to or have the time to. But the call might raise exceptions.

I've stumbled into this problem a few times and I'm sure it's a common problem which must have a common solution.

How do I safely call an async method without awaiting the result?

Update:

For people suggesting that I just await the result, this is code that is responding to a web request on our web service (ASP.NET Web API). Awaiting in a UI context keeps the UI thread free, but awaiting in a web request call will wait for the Task to finish before responding to the request, thereby increasing response times with no reason.

解决方案

If you want to get the exception "asynchronously", you could do:

  MyAsyncMethod().
    ContinueWith(t => Console.WriteLine(t.Exception),
        TaskContinuationOptions.OnlyOnFaulted);

This will allow you to deal with an exception on a thread other than the "main" thread. This means you don't have to "wait" for the call toMyAsyncMethod() from the thread that calls MyAsyncMethod; but, still allows you to do something with an exception--but only if an exception occurs.

Update:

technically, you could do something similar with await:

try
{
    await MyAsyncMethod().ConfigureAwait(false);
}
catch (Exception ex)
{
    Trace.WriteLine(ex);
}

...which would be useful if you needed to specifically use try/catch (or using) but I find the ContinueWith to be a little more explicit because you have to know what ConfigureAwait(false) means.

这篇关于如何安全地调用异步方法在C#没有等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆