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

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

问题描述

我有一个异步方法,它没有返回数据:

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

我是从它返回一些数据的另一种方法调用此

 公共字符串GetStringData()
{
    MyAsyncMethod(); //这将产生警告和燕子例外
    返回的Hello World;
}

调用 MyAsyncMethod()不必等待它会导致一个的因为这个呼叫没有等待,目前的方法继续完成调用之前运行,在Visual Studio中的警告。在该警告的页面就指出:


  

您应该考虑燮pressing警告只有当你确定你不想等待异步调用完成和调用的方法不会产生任何异常


我敢肯定,我不想等待呼叫完成;我并不需要或有时间。 但是调用的可能的引发异常。

我偶然到这个问题了几次,我敢肯定,这其中必须有一个共同的解决方案,一个共同的问题。

如何安全地调用异步方法,而不等待结果?

更新:

有关人士暗示,我只是等待结果,这是code,它是响应我们的Web服务(的ASP.NET Web API)的Web请求。在UI方面一直等待UI线程免费的,但在Web请求呼叫等待将等待任务以响应请求,因此,没有理由提高响应时间前完成。


解决方案

如果您想要得到的例外异步,你可以这样做:

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

这将使你处理比主线程的线程上的异常。这意味着你不必等待调用 MyAsyncMethod()从调用线程 MyAsyncMethod ;但是,仍然可以让你做一些与例外 - 但只有在发生异常

更新:

从技术上说,你可以做类似与的东西等待

 尝试
{
    等待MyAsyncMethod()ConfigureAwait(假)。
}
赶上(异常前)
{
    Trace.WriteLine(除息);
}

...这将是,如果你需要使用专门用尝试 / (或使用),但我找到 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#异步方法没有的await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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