tags:

views:

83

answers:

3

For instance... if i have a method that performs some asynchronous operation and i want to notify/call some sort of callback once it's done, would i use delegate?

I can't seem to get my head round how that would work. Can anyone point me in the right direction?

I was looking at something like....

public class ContentContext
{
        public delegate void SuccessCallback(Dictionary<String, String> content);

        public void DoSomeAsyncOpertaion(SuccessCallback successCallback)
        {
            //do something and then fire the callback
        }
}

But something about that smells funny. Still a beginner with this c# stuff so forgive my ignorance ;)

Cheers J

A: 

I think that's ok. You can also use the AsyncCallback Delegate.

bruno conde
+2  A: 

It's ok but the callback will be called on the same thread as the asynchronous operation.

If this is an issue, get some informations about Threads in .Net, SynchronizationContext, BackgroundWorder...

Guillaume
Good point on the callback thread.
Richard Szalay
+1  A: 

That looks fine to me. Though you could save yourself the delegate declaration and just use Action<>

I believe the official pattern is to use IAsyncResult, so I'd recommend that if you were developing a frameowrk.

Richard Szalay