Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Tip/Trick

Asynchronous execution with anonymous method pattern

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
15 Feb 2010CPOL 12.9K   8   3
This is my helper class: public class AsynchronousMethodHelper { private static Delegate _method; private static Delegate _callBack; public static void AsynchronousExecution(Delegate method, Delegate callBack) { _method = method; ...
This is my helper class:

public class AsynchronousMethodHelper
{
    private static Delegate _method;
    private static Delegate _callBack;

    public static void AsynchronousExecution(Delegate method, Delegate callBack)
    {
        _method = method;
        _callBack = callBack;
        BackgroundWorker bgWorker = new BackgroundWorker();
        bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
        bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        bgWorker.RunWorkerAsync();
    }

    private static void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        _callBack.DynamicInvoke(null);
    }

    private static void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        _method.DynamicInvoke(null);
    }
}


How to use:

FormProgressBar frmProgress = new FormProgressBar ();
frmProgress.Show();
AsynchronousMethodHelper.AsynchronousExecution(new ThreadStart(delegate()
{
    // anonymous  method for asynchronous execution
    for (int i = 0; i <= 2; i++)
    {
        // fake execution  (delay 2 seconds)
        Thread.Sleep(1000);
    }
}), new ThreadStart(delegate()
{
    // call back
    frmProgress .Hide();
}));

License

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


Written By
Thailand Thailand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis is a usefull article for me. Thank you very mych :) Pin
Asutosha19-Aug-10 4:02
professionalAsutosha19-Aug-10 4:02 
GeneralYou shouldn't use a static class Pin
Jeremy Hutchinson13-Jan-10 5:14
professionalJeremy Hutchinson13-Jan-10 5:14 
If you start one async method (method1) and start another async method (method2) while method1 is still running method1 calls back to method2's call back code.

If you remove all of the static modifiers and create and instance of the class it works great, and I will probably be making use of this in my code thanks.
GeneralRe: You also shouldn't leave it up to any Delegate Pin
kornman0013-Jan-10 5:23
kornman0013-Jan-10 5:23 

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.