.NET Delegates: A C# Bedtime Story

Asyncronous Notification

Async Notification: Fire & Forget

In the meantime, his boss and the universe have been distracted with other things, which means that the time it takes them to grade Peter's work is greatly expanded:

class Boss {
    public int WorkCompleted() {
        System.Threading.Thread.Sleep(3000);
        Console.WriteLine("Better..."); return 6; /* out of 10 */
    }
}
class Universe {
    static int WorkerCompletedWork() {
        System.Threading.Thread.Sleep(4000);
        Console.WriteLine("Universe is pleased with worker's work");
        return 7;
    }
    ...
}

Unfortunately, since Peter is notifying each listener one at a time, waiting for each to grade him, these notifications now take up quite a bit of his time when he should be working. So, he decides to forget the grade and just fire the event asynchronously:

    public void DoWork() {
        ...
        Console.WriteLine("Worker: work completed");
        if( completed != null ) {
            foreach( WorkCompleted wc in completed.GetInvocationList() )
            {
                wc.BeginInvoke(null, null);
            }
        }
    }

Async Notification: Polling

This allows Peter to notify the listeners while letting Peter get back to work immediately, letting the process thread pool invoke the delegate. Over time, however, Peter finds that he misses the feedback on his work. He knows that he does a good job and appreciates the praise of the universe as a whole (if not his boss specifically). So, he fires the event asynchronously, but polls periodically, looking for the grade to be available:

    public void DoWork() {
        ...
        Console.WriteLine("Worker: work completed");
        if( completed != null ) {
            foreach( WorkCompleted wc in completed.GetInvocationList() ) {
                IAsyncResult res = wc.BeginInvoke(null, null);
                while( !res.IsCompleted ) System.Threading.Thread.Sleep(1);
                int grade = wc.EndInvoke(res);
                Console.WriteLine("Worker grade= " + grade);
            }
        }
    }

Async Notification: Delegates

Unfortunately, Peter is back to what he wanted his boss to avoid with him in the beginning, i.e. looking over the shoulder of the entity doing the work. So, he decides to employ his own delegate as a means of notification when the async delegate has completed, allowing him to get back to work immediately, but still be notified when his work has been graded:

    public void DoWork() {
        ...
        Console.WriteLine("Worker: work completed");
        if( completed != null ) {
            foreach( WorkCompleted wc in completed.GetInvocationList() ) {
                wc.BeginInvoke(new AsyncCallback(WorkGraded), wc);
            }
        }
    }
    private void WorkGraded(IAsyncResult res) {
        WorkCompleted wc = (WorkCompleted)res.AsyncState;
        int grade = wc.EndInvoke(res);
        Console.WriteLine("Worker grade= " + grade);
    }

Happiness in the Universe

Peter, his boss and the universe are finally satisfied. Peter's boss and the universe are allowed to be notified of the events that interest them, reducing the burden of implementation and the cost of unnecessary round-trips. Peter can notify them each, ignoring how long it takes them to return from their target methods, while still getting his results asynchronously. Peter knows that it's not *quite* that easy, because as soon as he fires events asynchronously, the target methods are likely to be executed on another thread, as is Peter's notification of when the target method has completed. However, Peter's good friends with Mike, who is very familiar with threading issues and can provide guidance in that area.

And they a lived happily every after. The end.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The difference between theory and practice is smaller in theory than in practice.”