I understand that this question is almost one year old, but if the restrictions still exist, there is an option on .NET 3.5 that makes it easy to create asynchronous operations. Take a look at the Jeff Richter PowerThreading library . In the Wintellect.PowerThreading.AsyncProgModel namespace Wintellect.PowerThreading.AsyncProgModel you will find several variations of the AsyncEnumerator class that you can use with sequence generators to write async code as if it were sequential.
Its essence is that you write your asynchronous code as the body of a sequence generator that returns an IEnumerator<int> , and whenever you call the async method, you call yield return with the number of async operations to wait, the library processes the gory details.
For example, to send some data to a URL and return the contents of the result:
public IAsyncResult BeginPostData(string url, string content, AsyncCallback callback, object state) { var ae = new AsyncEnumerator<string>(); return ae.BeginExecute(PostData(ae, url, content), callback, state); } public string EndPostData(IAsyncResult result) { var ae = AsyncEnumerator<string>.FromAsyncResult(result); return ae.EndExecute(result); } private IEnumerator<int> PostData(AsyncEnumerator<string> ae, string url, string content) { var req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.BeginGetRequestStream(ae.End(), null); yield return 1; using (var requestStream = req.EndGetRequestStream(ae.DequeAsyncResult())) { var bytes = Encoding.UTF8.GetBytes(content); requestStream.BeginWrite(bytes, 0, bytes.Length, ae.end(), null); yield return 1; requestStream.EndWrite(ae.DequeueAsyncResult()); } req.BeginGetResponse(ae.End(), null); yield return 1; using (var response = req.EndGetResponse(ae.DequeueAsyncResult())) using (var responseStream = response.GetResponseStream()) using (var reader = new StreamReader(responseStream)) { ae.Result = reader.ReadToEnd(); } }
As you can see, the private PostData() process is responsible for the bulk of the work. There are three asynchronous programming methods, as evidenced by the three yield return 1 . With this template, you can bind as many async methods as you want, and just return one IAsyncResult caller.
source share