Click here to Skip to main content
15,867,686 members
Articles / General Programming / Threads

Asynchronous Thread Calls With Delegates

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
23 Mar 2012CPOL3 min read 36.4K   742   14   12
How to use delegates for async operations, pros and cons of it.

Introduction 

This is a small example for doing asynchronous operations on Windows Forms by using delegates.  As this article is mostly about using Asynchronous Thread Calls, there is nothing really fancy going on inside the application. But this article can be a useful piece of information in async operations. The required code explanatios can be found inside the project itself. 

Purpose, Pros, and Cons 

This actually is a different - and probably a rare approach for working on asynchronous operations. Basically, delegates are used to invoke different non-returning (void) functions. By doing this, you may achieve smoother and less "laggy" experience on your applications, when you need to make some heavy duty work such as large number calculations, massive I/O operations and even for working with web based applications (such as using web services etc.)

As a simple example, when calling loads of data from a database, it may take a while to load and show your data to user. In this scenario, user may think the application is in a deadlock or there has been a failure in the application. To prevent these, the user needs to see what's actually happening in the background. By using this method, the user will be able to make other operations (if possible) on the form, move the form itself, resize it and every other things that can be done.

However, there is one main drawback for this method, and that would be the part where Control.CheckForIllegalCrossThreadCalls = false; goes. Even though Microsoft has allowed developers to bypass Illegal Cross Thread Calls Checking, using it is considered as bad practice by most of the developers. But why it is a bad practice and what does it do actually?

At runtime phase of the application, .NET Framework checks if a control/object is being accessed and/or manipulated in an unsafe manner from another thread or threads. So basically, this controlling property comes in when there are multiple threads in an application. The property allows us to turn off the runtime check. That's why this is mostly considered as bad practice by most of the developers beacuse it is nothing but ignoring the problem that have or will occur. As most of us will agree, ignoring the problem is not the way to solve it.

So, what's the problem here? Actually there is no one problem. There might be all sorts of problems that can happen when multiple threads (or users) are trying to access and manipulate an object simultaneously, which is something even worse. So basically, the main problem is concurrency.

You can see the real trouble of illegal cross thread calls in the example below. 

Let's say we have a variable called result which its type is integer. The following code multiplies its value by 2 and writes the new value to the same variable. So, even if we have more than one thread and if the threads are executed sequentially (synchronously), we will not have any problems at all. The process steps are as shown below.

C#
result*=2;

However, if the threads are not executed sequentially, things are quite different...

As a conclusion, using Control.CheckForIllegalCrossThreadCalls is totally up to you and the structure of your application. Just to be safe, keep in mind that this property can also cause great security problems as well. So you better think twice before using it.

Using the code 

Mostly, the whole thing is working through delegates and simple methods. Delegates are used to send invoke messages. In this example, there are two main methods. The first one is DoStuff(), which obviously is for doing the async work, and the second one is EndProcess() which takes IAsyncResult object as parameter. In DoStuff() method, nothing fancy is given in the example. Only changing background color and dynamically inreasing value of a variable. 

C#
public void DoStuff()
{
    for (int i = 0; i...)
    {
         //...
    }
}

Nothing extra is used in the application like external resources, images etc. The source code or the demo application can be downloaded and run easily. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Software Engineer

Comments and Discussions

 
GeneralMy vote of 5 Pin
U@00726-Jul-12 22:57
U@00726-Jul-12 22:57 
QuestionBackgroundWorker? Pin
johannesnestler19-Mar-12 4:02
johannesnestler19-Mar-12 4:02 
AnswerRe: BackgroundWorker? Pin
radioman.lt19-Mar-12 6:01
radioman.lt19-Mar-12 6:01 
GeneralRe: BackgroundWorker? Pin
johannesnestler20-Mar-12 9:39
johannesnestler20-Mar-12 9:39 
GeneralRe: BackgroundWorker? Pin
Selim Sertaç BALCI20-Mar-12 9:45
Selim Sertaç BALCI20-Mar-12 9:45 
GeneralRe: BackgroundWorker? Pin
radioman.lt20-Mar-12 10:31
radioman.lt20-Mar-12 10:31 
GeneralRe: BackgroundWorker? Pin
johannesnestler21-Mar-12 8:24
johannesnestler21-Mar-12 8:24 
GeneralRe: BackgroundWorker? Pin
Selim Sertaç BALCI21-Mar-12 9:59
Selim Sertaç BALCI21-Mar-12 9:59 
GeneralRe: BackgroundWorker? Pin
johannesnestler22-Mar-12 0:43
johannesnestler22-Mar-12 0:43 
QuestionCode snippets with explanation Pin
Md. Marufuzzaman16-Mar-12 20:20
professionalMd. Marufuzzaman16-Mar-12 20:20 
QuestionMissing files Pin
Smitha Nishant16-Mar-12 12:18
protectorSmitha Nishant16-Mar-12 12:18 
AnswerRe: Missing files Pin
Selim Sertaç BALCI9-Aug-14 7:02
Selim Sertaç BALCI9-Aug-14 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.