这两天看MSDN的帮助文档看到下面这段代码
IAsyncResult factoryOpenResult = factory.BeginOpen(new AsyncCallback(OnOpenCompleteFactory), factory);
if (factoryOpenResult.CompletedSynchronously)
{
CompleteOpenFactory(factoryOpenResult);
}void OnOpenCompleteFactory(IAsyncResult result)
{
if (result.CompletedSynchronously)
return;
else
CompleteOpenFactory(result);
}void CompleteOpenFactory(IAsyncResult result)
{
IChannelFactory<IDuplexSessionChannel> factory =
(IChannelFactory<IDuplexSessionChannel>)result.AsyncState; factory.EndOpen(result);
.........
}
不明白的是在这里为什么要判断factoryOpenResult.CompletedSynchronously以及result.CompletedSynchronously
如果不做任何判断异步调用也是可以完成的呀
希望高手作答 谢谢了
IAsyncResult factoryOpenResult = factory.BeginOpen(new AsyncCallback(OnOpenCompleteFactory), factory);
if (factoryOpenResult.CompletedSynchronously)
{
CompleteOpenFactory(factoryOpenResult);
}void OnOpenCompleteFactory(IAsyncResult result)
{
if (result.CompletedSynchronously)
return;
else
CompleteOpenFactory(result);
}void CompleteOpenFactory(IAsyncResult result)
{
IChannelFactory<IDuplexSessionChannel> factory =
(IChannelFactory<IDuplexSessionChannel>)result.AsyncState; factory.EndOpen(result);
.........
}
不明白的是在这里为什么要判断factoryOpenResult.CompletedSynchronously以及result.CompletedSynchronously
如果不做任何判断异步调用也是可以完成的呀
希望高手作答 谢谢了
效果应该是相同的
IAsyncResult factoryOpenResult = factory.BeginOpen(new AsyncCallback(CompleteOpenFactory), factory);
void CompleteOpenFactory(IAsyncResult result)
{
IChannelFactory <IDuplexSessionChannel> factory =
(IChannelFactory <IDuplexSessionChannel>)result.AsyncState; factory.EndOpen(result);
.........
}
{
CompleteOpenFactory(factoryOpenResult); 如果是异步完成的就由回调函数来执行。这个属性作用不大,因为一般来说
}
异步操作都是从线程池中新开线程执行,没必要判断。
就在当前的线程中调用CompleteOpenFactory(),也就相当于是一个同步调用
与此同时 在异步调用的函数OnOpenCompleteFactory中的result.CompletedSynchronously也必然是True
因此在此函数中直接return不再调用CompleteOpenFactory()
不知道我的理解是否正确如果是这样的话,个人意见认为这个操作没有什么必要