private void cmdAsync_Click(object sender, System.EventArgs e) { // Create the delegate GreetingDelegate delg = new GreetingDelegate(GetGreeting); // Make the async call int threadId = 0; int delay = Convert.ToInt32(txtDelay.Text); IAsyncResult ar = delg.BeginInvoke(txtName.Text, delay, out threadId, new AsyncCallback(CallbackMethod), delg); }
static void Main(string[] args) { RemotingConfiguration.Configure("HelloClient.exe.config"); Hello obj = new Hello(); if (obj == null) { Console.WriteLine("could not locate server"); return; } // synchronous: // greeting = obj.Greeting("Christian"); // asynchronous: GreetingDelegate d = new GreetingDelegate(obj.Greeting); Console.WriteLine("Starting remote method"); IAsyncResult ar = d.BeginInvoke("Christian", null, null); // do some work Console.WriteLine("Do some other work"); ar.AsyncWaitHandle.WaitOne(); if (ar.IsCompleted) { greeting = d.EndInvoke(ar); } Console.WriteLine(greeting); /* * Console.WriteLine("starting long call"); * obj.TakeAWhile(10000); * Console.WriteLine("finished long call"); */ }
static void Main(string[] args) { RemotingConfiguration.Configure("HelloClient.exe.config"); Hello obj = new Hello(); if (obj == null) { Console.WriteLine("could not locate server"); return; } // synchronous: // greeting = obj.Greeting("Christian"); // asynchronous: GreetingDelegate d = new GreetingDelegate(obj.Greeting); Console.WriteLine("Starting remote method"); IAsyncResult ar = d.BeginInvoke("Christian", null, null); // do some work Console.WriteLine("Do some other work"); ar.AsyncWaitHandle.WaitOne(); if (ar.IsCompleted) { greeting = d.EndInvoke(ar); } Console.WriteLine(greeting); /* Console.WriteLine("starting long call"); obj.TakeAWhile(10000); Console.WriteLine("finished long call"); */ }
public IAsyncResult BeginGreet(string name, AsyncCallback callback, object state) { GreetingDelegate caller = new GreetingDelegate(() => Greet(name)); return caller.BeginInvoke(callback, state); }