Here I demonstrate two simple ways to execute a method asynchronously in .NET app.
A. Using AsyncWaitHandle
This is a very useful way to allow your application to process other task. I used this technique most when loading data in my UI, like, loading data into the dropdownlist simultaneously.
1. Create a delegate that will handle the asynchronous request
public delegate CountryReadOnlyCollection LoadList();
2. Use the delegate to execute asynchronous request
LoadList loadList = new LoadList(CountryReadOnlyCollection.GetList); IAsyncResult r = loadList.BeginInvoke(null, null); // Do several task here... // WaitOne will wait untill the task completed before proceding to the next step r.AsyncWaitHandle.WaitOne(); CountryReadOnlyCollectionlist = loadList.EndInvoke(r); Trace.WriteLine(string.Format("Total Count: {0}", list.Count));
Leave a comment