我最近注意到了以下的模式,但我並不完全掌握CompletedSynchronously屬性的用法:再次的AsyncCallback CompletedSynchronously
IAsyncResult channelOpenResult = channel.BeginOpen(new AsyncCallback(OnOpenCompleteChannel), channel);
if (channelOpenResult.CompletedSynchronously)
{
CompleteOpenChannel(channelOpenResult);
}
,然後在回調:
void OnOpenCompleteChannel(IAsyncResult result)
{
if (result.CompletedSynchronously)
return;
else
CompleteOpenChannel(result);
}
代碼中的某處當然有一個功能:
void CompleteOpenChannel(IAsyncResult result) ...
這是一種處理方法異步調用取決於它是否直接完成?但是爲什麼在這種情況下使用它,因爲AsyncCallback總是會被調用(是嗎?)? 有人可以舉一個例子,呼叫是同步進行的嗎?