I have a function which created a delegated and starts BeginInvoke on that object, with another function being passed in to wait for EndInvoke:
private static void DeploymentComponentThreadedCallBack(IAsyncResult ar)
{
var result = (AsyncResult)ar;
var pluginExecuteAction = (Action<int, Guid, int, EnvironmentServerComponentSet, string>)result.AsyncDelegate;
pluginExecuteAction.EndInvoke(ar);
//report back to WCF service that thread is finished
}
public void DeployComponent(byte[] resource, Guid componentGuid, string deploymentType, Dictionary<string, object> args)
{
var asyncCallback = new AsyncCallback(DeploymentComponentThreadedCallBack);
IDeployComponent plugin = GetPluginDelegate();
Action<byte[], Guid, string, Dictionary<string, object>> pluginExecuteAction = plugin.DeployComponent;
IAsyncResult ar = pluginExecuteAction.BeginInvoke(resource, componentGuid, deploymentType, args, asyncCallback, null);
}
I’d like to unit test this, but when I do so, DeploymentComponentThreadedCallBack never gets hit, and obviously neither does the EndInvoke call. I presume this is happening because the test passes before the asynchronous thread ends, so the thread stops executing before EndInvoke, but is there a way I can stop this happening so I can see that EndInvoke gets hit?
Cheers,
Matt
I think your basic problem is that you’re not exposing anything on the
DeployComponentmethod that would let you track the asynchronous operation that you’re starting there. If you returned theIAsyncResultfrom there you could callar.AsyncWaitHandle.WaitOne()to wait until it completed.