Introduction
Nowadays, applications often include heavy processing like scientific calculations, backing up or restoring databases, multi-level cross tab reporting, summarizing data, file or directory search, and so forth. While performing such operations, the CPU can get fully occupied in the task, thus making the user interface unresponsive. Consider a case where a method is written to populate the items of a list box using a long-running loop.
Source Code 1 shows the code for this task:
- private void ExecuteTask() {
- for (long i = 1; i < 100000000000; i++) {
- lstCustomers.items.Add(i.ToString());
- }
- }
Source Code 2 invokes the method in source code 1 on the click event of btnStart control
- private void btnStart_Click(object sender, EventArgs e) {
- ExecuteTask();
- MessageBox.show(“Finished”);
- }
Upon execution of this application, the system might take a long time to process the information to be displayed in the list box. In such a case, it is necessary to inform the user about the status of the application. Also, it may be more sensible to assign this task to the background, while keeping the user interface responsive and continuing to run other tasks in the foreground.
Asynchronous Programming
Asynchronous programming is a concept where time-consuming applications run in the background while other tasks are performed simultaneously. However, even if an operation is performed in the background, the user needs to be informed of the status. A recommended method would be to use a label or a text box or a status bar showing the status of the process to the user.
An example for such a scenario would be a file or a directory search operation where a text box or a label control can be used to show the current status of the search operation. Another example would be CD-burning software. Various chat applications like Yahoo or MSN Messenger make use of the status bar to let the user know the status of an operation. The progress bar control can also show the progress of an operation which takes a long time for execution. Such systems that respond to user inputs while carrying on simultaneous background operations are also called interactive systems.
Asynchronous programming uses only one thread and still allows background tasks to take place. They also follow a linear pattern of execution and keep the user interface responsive.
Although asynchronous programming uses only one thread and still allows background tasks to take place. They also follow a linear pattern of execution and keep the user interface responsive.
Although asynchronous programming offers several benefits to developers It must not be used randomly. It can affect the performance of applications if used unnecessarily.
Asynchronous Programming using Delegates
Developers commonly use delegates to invoke the asynchronous method in .NET. The signature of a delegate should always match the signature of the method. For example, if there is a method called LongTask() that takes an int as a parameter and returns an integer, a delegate should be defined as shown in the code below.
- public delegate int LongTaskDelegate(int time, ref int refParam);
When a delegate is defined, the common language runtime(CLR) automatically defines BeginInvoke() and EndInvoke() methods on that delegate, which can be called to execute the referenced methods asynchronously.

Hamid GhaffariPosted Mar 4, 2021, 6:06 PM
Pannel_sell