Call BeginInvoke on a Delegate

T

Thomas Lerchner

Hi!

I try to call the BeginInvoke method of a delegate but I get an Exception
"NotSupportedException" when I
execute the program. Here is the code I'm using it is from the MS
helpsystem.

Thankx Thomas

TMS Technical Management Systems GmbH
www.t-m-s.at

using System;
using System.Threading;

public class AsyncDemo
{
// The method to be executed asynchronously.
public string TestMethod ( int callDuration )
{
Console.WriteLine ( "Test method begins." );

Thread.Sleep ( callDuration );

return "MyCallTime was " + callDuration.ToString ();

}

}

// The delegate must have the same signature as the method

// you want to call asynchronously.

public delegate string AsyncDelegate ( int callDuration );

public class AsyncMain

{

static void Main ( string[] args )

{

// Create an instance of the test class.

AsyncDemo ad = new AsyncDemo();

// Create the delegate.

AsyncDelegate dlgt = new AsyncDelegate ( ad.TestMethod );


// Initiate the asychronous call.

IAsyncResult ar = dlgt.BeginInvoke ( 3000, null, null );

Thread.Sleep ( 0 );

Console.WriteLine ( "Main thread does some work." );

// Call EndInvoke to Wait for the asynchronous call to complete,

// and to retrieve the results.

string ret = dlgt.EndInvoke ( ar );

Console.WriteLine("The call executed on thread with return value \"{0}\".",
ret);

Console.ReadLine ();

}

}
 
A

Alex Yakhnin [eMVP]

AsyncDelegates are supported only in the few scenarios in
the current version of CF: Sockets and Async. calls to
WebServices.

-Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top