Asynchronous Methods and Delegates

  • Thread starter Thread starter theBoringCoder
  • Start date Start date
T

theBoringCoder

Hello All,

I just started learning about Delegates, and was wondering
if someone could point me at some useful, straightforward
examples of how to use delegates to make method calls
asynchronously. I've been told that this is possible, and
works similar to callbacks in C++, but cannot find any
straightforward examples in C#.

Thanks,

theBoringCoder
 
theBoringCoder,

It's actually quite easy. If you create a delegate like this:

public delegate void MyMethodDelegate(int myParam);

Then an instance of a delegate exposes three methods, Invoke,
BeginInvoke, and EndInvoke. What you want to do is create an instance of
the delegate, and then call the BeginInvoke method, like so:

// Create the delegate.
MyMethodDelegate pobjDelegate = new MyMethodDelegate(MyMethod);

// Call asynchronously.
IAsyncResult pobjResult = pobjDelegate.BeginInvoke(1, null, null);

For a more detailed explaination and examples, check out the section of
the .NET framework documentation titled "Asynchronous Delegates", located at
(watch for line wrap):

http://msdn.microsoft.com/library/d...s/cpguide/html/cpovrasynchronousdelegates.asp

Hope this helps.
 
Thanks. That helps a lot.
-----Original Message-----
theBoringCoder,

It's actually quite easy. If you create a delegate like this:

public delegate void MyMethodDelegate(int myParam);

Then an instance of a delegate exposes three methods, Invoke,
BeginInvoke, and EndInvoke. What you want to do is create an instance of
the delegate, and then call the BeginInvoke method, like so:

// Create the delegate.
MyMethodDelegate pobjDelegate = new MyMethodDelegate (MyMethod);

// Call asynchronously.
IAsyncResult pobjResult = pobjDelegate.BeginInvoke(1, null, null);

For a more detailed explaination and examples, check out the section of
the .NET framework documentation titled "Asynchronous Delegates", located at
(watch for line wrap):

http://msdn.microsoft.com/library/default.asp? url=/library/en-
us/cpguide/html/cpovrasynchronousdelegates.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello All,

I just started learning about Delegates, and was wondering
if someone could point me at some useful, straightforward
examples of how to use delegates to make method calls
asynchronously. I've been told that this is possible, and
works similar to callbacks in C++, but cannot find any
straightforward examples in C#.

Thanks,

theBoringCoder


.
 
Back
Top