I have a library that uses the BeginXxx EndXxx asynchronous pattern (obviously, the following code is simplified):
ILibrary
{
IAsyncResult BeginAction(string name, AsyncCallback callback, object state);
int EndAction(IAsyncResult asyncResult, out object state)
}
I'm trying to build a class that uses that library, and that implements the same pattern itself:
Manager
{
private ILibrary library;
public IAsyncResult BeginMultipleActions(IEnumerable<string> names,
AsyncCallback callback, object state)
{
var handles = new List<IAsyncResult>();
foreach(string name in names)
{
var handle = library.BeginAction(name, OnSingleActionCompleted, null);
handles.Add(handle);
}
//???
}
public int EndMultipleActions(IAsyncResult asyncResult, out object state)
{
//???
}
private void OnSingleActionCompleted(IAsyncResult asyncResult)
{
//???
}
}
I've tried a couple of solutions (using ManualResetEvent) but I find my code very ugly. What is the cleanest way to do this (in C#3.5) without losing functionality (callback, wait handle, ...)?