【发布时间】:2011-09-01 00:25:14
【问题描述】:
我有一个函数创建了一个委托并在该对象上启动 BeginInvoke,另一个函数被传入以等待 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);
}
我想对此进行单元测试,但是当我这样做时,DeploymentComponentThreadedCallBack 永远不会受到打击,显然 EndInvoke 调用也不会。我认为发生这种情况是因为测试在异步线程结束之前通过,因此线程在 EndInvoke 之前停止执行,但是有没有办法可以阻止这种情况发生,以便看到 EndInvoke 被命中?
干杯, 马特
【问题讨论】:
标签: c# multithreading delegates