ConfigureAwait क्या है
ConfigureAwait किसी program को बताता है कि इंतज़ार के बाद उसे बिल्कुल वहीं वापस जाने की ज़रूरत नहीं जहां उसने शुरू किया था, जैसे break के बाद अपनी वही seat पर वापस जाने की ज़रूरत न होना। यह कुछ code को ज़्यादा efficient बना सकता है।
In this page:
Syntax
await SomeAsyncOperation().ConfigureAwait(false); // do not resume on the original context
ConfigureAwait
ConfigureAwait(false) await को original synchronization context (जैसे एक UI thread) पर वापस execution फिर से शुरू करने की कोशिश न करने के लिए कहता है, जो library code में performance सुधार सकता है और deadlocks से बचा सकता है।
Console apps और modern ASP.NET Core में वापस जाने के लिए कोई special context नहीं होता, इसलिए वहां इसका practical फर्क कम है। यह मुख्यतः एक library-authoring best practice है, न कि कुछ जो application code को हर जगह चाहिए।
Note:
Reusable library code में
ConfigureAwait(false) का उपयोग करें जिसे किसी specific UI thread पर निर्भर रहने की कोई वजह न हो।उदाहरण: ConfigureAwait
using System;
using System.Threading.Tasks;
class Program
{
static async Task<int> LoadDataAsync()
{
await Task.Delay(50).ConfigureAwait(false);
return 7;
}
static async Task Main(string[] args)
{
int value = await LoadDataAsync();
Console.WriteLine(value);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- ऐसे code में
ConfigureAwait(false)उपयोग करना जो बाद में UI controls को छूता है, जो गलत thread पर fail हो जाता है। - यह सोचना कि यह अपने आप method को तेज़ बना देता है।
- यह भूल जाना कि यह सिर्फ उस
awaitको प्रभावित करता है जिससे यह जुड़ा है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: