久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在不等待的情況下在 C# 中安全地調用異步方

How to safely call an async method in C# without await(如何在不等待的情況下在 C# 中安全地調用異步方法)
本文介紹了如何在不等待的情況下在 C# 中安全地調用異步方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個不返回數據的 async 方法:

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

調用 MyAsyncMethod() 而不等待它會導致 "因為沒有等待這個調用,所以當前方法在調用完成之前繼續運行"視覺工作室中的警告.在該警告的頁面上,它指出:

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:

只有在確定不想等待異步調用完成并且被調用的方法不會引發任何異常時,才應考慮取消警告..p>

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.

如何在不等待結果的情況下安全地調用異步方法?

對于建議我只是等待結果的人,這是響應我們 Web 服務 (ASP.NET Web API) 上的 Web 請求的代碼.在 UI 上下文中等待使 UI 線程保持空閑,但在 Web 請求調用中等待將等待任務完成后再響應請求,從而無緣無故地增加響應時間.

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

這將允許您處理除主"線程之外??的線程上的異常.線.這意味著您不必等待".用于從調用 MyAsyncMethod 的線程調用 MyAsyncMethod();但是,仍然允許您在異常情況下執行某些操作——但前提是發生異常.

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 to MyAsyncMethod() from the thread that calls MyAsyncMethod; but, still allows you to do something with an exception--but only if an exception occurs.

從技術上講,你可以用 await 做類似的事情:

technically, you could do something similar with await:

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

...如果您需要專門使用 try/catch(或 using),這將很有用,但我發現 ContinueWith 更明確一點,因為您必須知道 ConfigureAwait(false) 表示.

...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# 中安全地調用異步方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Create a summary description of a schedule given a list of shifts(給定輪班列表,創建時間表的摘要描述)
C# Normal Random Number(C# 普通隨機數)
Standard deviation of generic list?(通用列表的標準偏差?)
AsyncCTP: Creating a class that is IAwaitable(AsyncCTP:創建一個 IAwaitable 的類)
主站蜘蛛池模板: 伊人狠狠干 | 国产亚洲一区二区精品 | 日韩中文字幕在线观看视频 | 国产小视频在线 | 羞羞视频在线观看网站 | 精品国产乱码久久久久久久久 | 成人福利片 | 欧美大片一区 | 国产色婷婷精品综合在线播放 | 国产精品 亚洲一区 | 国产精品久久久久久久岛一牛影视 | 神马影院一区二区三区 | 久久99精品久久久久久秒播九色 | 国产亚洲成av人片在线观看桃 | 国产99热精品 | 国产情侣一区 | 色视频在线播放 | 国产一区二 | 中文字幕精品视频 | 欧美美女一区二区 | 日韩一区二区三区在线观看 | 国产男女猛烈无遮掩视频免费网站 | 国产小视频在线观看 | 91黄色片免费看 | 综合色播 | 日本久久网 | 中文字幕一区二区三区四区五区 | 曰韩三级| 国产中文视频 | 午夜成人免费视频 | 国产精品久久久久久亚洲调教 | 亚洲视频在线一区 | 国产精品日本一区二区不卡视频 | 国产精品美女久久久久久免费 | 一区二区三区欧美在线 | 成年人黄色一级片 | 日日摸夜夜爽人人添av | 日韩精品在线播放 | 国产欧美精品一区二区色综合 | 91精品国产综合久久久久久漫画 | 97久久久久久久久 |