Handling Asynchronous Operations c# In Hindi

Handling Asynchronous Operations c# In Hindi

C# प्रोग्रामिंग लैंग्वेज में एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन को हैंडल या मैनेज करने का अर्थ है, की मौजूदा प्रोग्राम में मेन थ्रेड को ब्लॉक किए बिना टास्क प्रोसेस को एक्ज़ीक्यूट या रन करना। इस प्रोसेस में प्रोग्राम एप्लिकेशन में अधिक टाइम लेने वाले प्रोग्राम सीक्वेंस टास्क ऑपरेशन एक्टिविटीज परफॉर्म करते समय भी रिस्पॉन्सिव स्टेप बाय स्टेप मेन्टेन रहते हैं.

Handling Asynchronous Operations c# In Hindi

Asynchronous program task operations in the C# include.

  • डेटाबेस में कनेक्टेड डाटा या फ़ाइल को रीड /राइट ऑपरेशन परफॉर्म करना।
  • कंप्यूटर डेटाबेस मे स्टोर डाटा को एक्सेस और मैनेज करना।
  • जरूरी सिस्टम एपीआई कॉल और प्रोसेस करना।
  • नेटवर्क कम्युनिकेशन इस्टैब्लिशमेंट करना।
  • मौजूदा सिस्टम में बैकग्राउंड सिस्टम प्रोसेसिंग टास्क करना।

For asynchronous program task operations, C# uses.

  • async कीवर्ड
  • await कीवर्ड
  • Task सिस्टम डिफाइन टास्क
  • Task<T> टास्क टाइप

ये सभी रिजर्व्ड कीवर्ड मौजूदा प्रोग्राम में एसिंक्रोनस टास्क एक्टिविटीज ऑपरेशन को प्रॉपर मैनेज करने के लिए यूज़ किए जाते है।

What is an asynchronous operation in C#?

C# प्रोग्रामिंग में एक एसिंक्रोनस ऑपरेशन मौजूदा प्रोग्राम को कम्पलीट प्रोसेस होने का वेट करने के लिए फाॅर्स किए बिना आटोमेटिक रन होता रहता है।

यह किसी प्रोग्राम में टास्क एक्ज़ीक्यूशन स्टॉप करने के बदले, प्रोग्राम या एप्लिकेशन या अन्य दूसरे टास्क को कंटिन्यू रख सकता है।

Why handle asynchronous operations in C#?

Without asynchronous programming tasks.

  • एसिंक्रोनस ऑपरेशन के बिना आपके प्रोग्राम या एप्लीकेशन फ़्रीज़ हो सकते हैं.
  • एसिंक्रोनस ऑपरेशन टास्क के आपके UI रिस्पॉन्सिव नहीं रहता है.
  • बिना एसिंक्रोनस ऑपरेशन के वेब सर्वर का परफ़ॉर्मेंस स्लो या कम हो जाती है

With asynchronous programming tasks.

  • एसिंक्रोनस ऑपरेशन के साथ आपको इसमें बेहतर रिस्पॉन्सिवनेस बिहेवियर मिलता है.
  • एसिंक्रोनस ऑपरेशन के साथ आपको इसमें बेहतर स्केलेबिलिटी फीचर्स मिलते है.
  • एसिंक्रोनस ऑपरेशन के साथ यह एफ़िशिएंट रिसोर्स या यूज़फुल होता है.

Basic asynchronous programming task structure.

async Task MethodName()

{

await AnyOperation();

}

Example of handling an asynchronous operation.

using System;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        Console.WriteLine(“Application Begin”);

        await ManageData();

        Console.WriteLine(“Application Terminated”);

    }

    static async Task ManageData()

    {

        Console.WriteLine(“Processing Begin”);

        await Task.Delay(5000);

        Console.WriteLine(“Processing Terminated”);

    }

}

Output is.

Application Begin

Processing Begin

(5 second pause)

Processing Terminated

Application Terminated

Explaining handling an asynchronous operation.

  • यहाँ इस प्रोग्राम में Main() फंक्शन मेथड को async के रूप में इंडीकेट किया गया है.
  • यहाँ ManageData() एक एसिंक्रोनस प्रोग्राम ऑपरेशन को परफॉर्म करता है.
  • यहाँ await Task.Delay(5000) एक लॉन्ग टाइम तक रन होने वाले टास्क प्रोसेस को सिमुलेट करता है
  • यहाँ await कीवर्ड सिर्फ़ टास्क मेथड को पॉज़ करता है, कम्पलीट प्रोग्राम या एप्लिकेशन को नहीं करता है।
  • प्रोग्राम टास्क कम्पलीट होने के बाद प्रोग्राम एप्लीकेशन एग्ज़िक्यूशन फिर से स्टार्ट होता है.

Returning data from an async task operation.

किसी प्रोग्राम में टास्क वैल्यू को रिटर्न करते समय Task<T> मेथड कीवर्ड का यूज़ करें।

Example of returning data from an async task.

using System;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        int output = await CalculateMul();

        Console.WriteLine(“The Multiplication is = ” + output);

    }

    static async Task<int> CalculateMul()

    {

        await Task.Delay(5000);

        return 5 * 3; // The result is – 15

    }

}

Output is.

The Multiplication is = 15

Handling multiple asynchronous task operations in C# programming.

C# प्रोग्राम में एक साथ कई एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन रन हो सकते है। इन्हे वन बाय वन स्टेप में रन या एक्सेक्यूट किया जाता है.

Example of using Task.WhenAll in a C# program.

using System;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        Task task1 = FileDownload();

        Task task2 = FileUpload();

        await Task.WhenAll(task1, task2);

        Console.WriteLine(“All File Download/ Upload Operations Done”);

    }

    static async Task FileDownload()

    {

        await Task.Delay(5000);

        Console.WriteLine(“File Download 100% Done”);

    }

    static async Task FileUpload()

    {

        await Task.Delay(3000);

        Console.WriteLine(“File Upload 100% Done”);

    }

}

Output is.

(3 sec)

File Upload 100% Done

(5 sec)

File Download 100% Done

All File Download/ Upload Operations Done

Handling exceptions in async methods in C#.

C# प्रोग्राम में एक्सेप्शन एरर हैंडलिंग करने के लिए try-catch ब्लॉक का यूज़ किया जाता है।

Example of handling exceptions in async methods.

using System;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        try

        {

            await DivideIntegers();

        }

        catch (Exception exception)

        {

            Console.WriteLine(“Display Error – ” + exception.Message);

        }

    }

    static async Task DivideIntegers()

    {

        await Task.Delay(3000);

        int p = 13;

        int q = 0;

        int division = p / q;

    }

}

Example of using the Task.Run method.

using System;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        await Task.Run(() =>

        {

            for (int p = 1; p <= 9; p++)

            {

                Console.WriteLine(p);

            }

        });

        Console.WriteLine(“System Generated Task Terminated”);

    }

}

output is.

1 2 3 4 5 6 7 8 9

System Generated Task Terminated

Best practices for asynchronous programs.

  • C# प्रोग्रामिंग लैंग्वेज में एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन को हैंडल या मैनेज करने async और await कीवर्ड मेथड को प्रायोरिटी प्रोवाइड करे.
  • मौजूदा प्रोग्राम में थ्रेड्स को मैन्युअल रूप से मैनेज करना अवॉयड करे।

Avoid blocking calls in asynchronous programs.

Do not use this in your program.

  • task.Wait();
  • task.Result;
  • Use this in your program.
  • await task;

Use Task.WhenAll for parallel asynchronous program task operations.

इससे आपके प्रोग्राम की परफॉर्मेंस बेहतर होती है।

Handle asynchronous program task exception errors properly.

अपने प्रोग्राम में हमेशा एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन को हैंडल या मैनेज करने के  लिए try-catch ब्लॉक मेथड का यूज़ करें।

Advantages of asynchronous operation tasks in C#.

  • अपने प्रोग्राम एप्लीकेशन में एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन से आपको बेहतर एप्लीकेशन रिस्पॉन्सिवनेस फीचर्स मिलता है.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन आपके सिस्टम एफ्फिसिएंट CPU का यूज़ या मैनेजमेंट करता है.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन आपको बेहतर प्रोग्राम टास्क स्केलेबिलिटी फीचर प्रोवाइड करते है.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन आपको नॉन-ब्लॉकिंग एग्जीक्यूशन प्रोवाइड करता है.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन आपको बेहतर यूज़र एक्सपीरियंस प्रोवाइड करता है.

Disadvantages of asynchronous operation tasks in C#.

  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन में आपको काम्प्लेक्स प्रोग्राम एरर सलूशन या डिबगिंग टास्क मिलते है.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन के फाल्स यूज़ से आपके प्रोग्राम में डेडलॉक डेवलप हो सकते हैं.
  • एसिंक्रोनस प्रोग्राम टास्क ऑपरेशन का नए यूजर प्रोग्रामर के लिए इसे समझना और यूज़ करना थोड़ा मुश्किल हो सकता है.

Real-life example of asynchronous operation tasks.

ऑनलाइन वेब सर्वर या इंटरनेट से जुड़े डेडिकेटेड वेब सर्वर से फ़ाइल डाउनलोड या अपलोड करना।

  • Synchronous task – इस प्रोसेस में डेडिकेटेड वेब सर्वर से फाइल डाउनलोड टर्मिनेट होने तक प्रोग्राम एप्लीकेशन फ़्रीज़ हो जाता है.
  • Asynchronous task – इस प्रोसेस में यूज़र बैकग्राउंड में डाउनलोड होने के दौरान भी आपके प्रोग्राम या एप्लीकेशन में इसका यूज़ कंटिन्यू रहता है.

Popular Methods for Async Application Operations.

#Task Method NameWhy use or Purpose
1.Task.Delay()This method used to Delays program application execution asynchronously when need
2.Task.WhenAll()This method used to Waits for all tasks system or user created task
3.Task.WhenAny()This method used to Waits for the given time by user when first completed task
4.Run()This method used to Runs program code on a background thread

Summary of Handling Asynchronous Operations.

  • C# प्रोग्रामिंग लैंग्वेज में एसिंक्रोनस प्रोग्राम ऑपरेशन को हैंडल या मैनेज करने से प्रोग्राम में मेन थ्रेड को ब्लॉक किए बिना लॉन्ग टाइम तक रन होने वाले टास्क को परफॉर्म कर सकते हैं। इस प्रोसेस में मुख्य रूप से शामिल हैं, जिसमे async, await,Task, Task<T>, Task.WhenAll, आदि रिजर्व्ड कीवर्ड का यूज़ कर प्रोग्राम में Synchronous, Asynchronous, आदि टास्क को आसानी से परफॉर्म किया जा सकता है.
  • किसी प्रोग्राम में async मेथड का यूज़ कर काम्प्लेक्स प्रोग्राम एक्सेप्शन एरर हैंडलिंग किया जा सकता है.
  • आज के समय में एसिंक्रोनस प्रोग्रामिंग मॉडर्न प्रोग्राम एप्लिकेशन के लिए एक एसेंशियल टास्क है. क्योंकि यह आपके प्रोग्राम एप्लीकेशन के लिए एक रिस्पॉन्सिवनेस, स्केलेबिलिटी, और परफॉर्मेंस को इम्प्रूव करता है।

Leave a Reply