With the help of delegate you can call "BeginInvoke" on any function.. By calling BeginInvoke you make the call to your function asynchronous...
-
using System.Threading;
-
using System.Runtime.Remoting.Messaging;
-
class Program
-
{
-
public delegate String AsyDel();
-
public AsyDel delAsy;
-
-
static void Main(string[] args)
-
{
-
Program p=new Program();
-
-
p.delAsy=new AsyDel(p.Some);
-
//Define a delegate to call "ANY" function Asynchronous...
-
// Delegate should have same signature...
-
-
p.delAsy.BeginInvoke(new AsyncCallback(p.After_Some),null);
-
//
-
-
-
-
Console.ReadLine();
-
-
-
Console.WriteLine("After ");
-
-
}
-
-
public string Some()
-
{
-
Console.WriteLine("Doin Something Asy");
-
Thread.Sleep(1000);
-
return "C#";
-
-
-
}
-
-
public void After_Some(IAsyncResult res)
-
{
-
AsyncResult ar = (AsyncResult)res;
-
string ss="Def";
-
-
-
-
delAsy = (AsyDel)ar.AsyncDelegate;
-
-
try
-
{
-
ss = delAsy.EndInvoke(res);
-
}
-
-
catch (Exception ex)
-
{
-
Console.WriteLine(ex.Message.ToString());
-
}
-
-
Console.WriteLine(ss);
-
-
}
-
}
-
Here in the above prog... public string Some() is a function that returns string... Normally the call would be "linear"... that is the main thread callin the function and blocking until the function is finished...
However here we have done it Asynchronously.. As soon as the function has completed you get notified as "After_Some" is called...
Simply replace the call to function with call to web service...You get the picture.. Web service may/ maynot respond.. still your application wont hang.. or crash...