← Back to C# Course | Chapter 13: Async Programming | Lesson 4 of 6

ConfigureAwait क्या है

ConfigureAwait किसी program को बताता है कि इंतज़ार के बाद उसे बिल्कुल वहीं वापस जाने की ज़रूरत नहीं जहां उसने शुरू किया था, जैसे break के बाद अपनी वही seat पर वापस जाने की ज़रूरत न होना। यह कुछ code को ज़्यादा efficient बना सकता है।

In this page:

  1. ConfigureAwait
Syntax
csharp
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

markup
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);
    }
}
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. #}
आम गलतियां
  1. ऐसे code में ConfigureAwait(false) उपयोग करना जो बाद में UI controls को छूता है, जो गलत thread पर fail हो जाता है।
  2. यह सोचना कि यह अपने आप method को तेज़ बना देता है।
  3. यह भूल जाना कि यह सिर्फ उस await को प्रभावित करता है जिससे यह जुड़ा है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.