Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncCallBack CompletedSynchronously

I've noticed the following pattern recently, but I don't entirely grasp the usage of the CompletedSynchronously property:

IAsyncResult channelOpenResult = channel.BeginOpen(new AsyncCallback(OnOpenCompleteChannel), channel);
if (channelOpenResult.CompletedSynchronously)  
{  
    CompleteOpenChannel(channelOpenResult);  
}

And then again, in the callback:

void OnOpenCompleteChannel(IAsyncResult result)  
{  
    if (result.CompletedSynchronously)  
        return;  
    else  
        CompleteOpenChannel(result);  
}

And somewhere in the code there is of course a function:

void CompleteOpenChannel(IAsyncResult result) ...

Is this a way to handle the asynchronous call differently depending on whether it completes directly or not? But why use it in this case, since the AsyncCallback always will be called (will it?)? Could someone give an example where the call is made synchronously?

like image 953
Jonatan Lindén Avatar asked Sep 03 '09 08:09

Jonatan Lindén


1 Answers

See this blog. A common pattern does async work in a loop, and checking CompletedSynchronously helps avoid the case where you get 'unlucky' and a bunch of async calls happen to complete sync and you risk StackOverflowException. (E.g. if you're reading data over the network, and the data you're reading has already come over the wire and is buffered, your async call may complete synchronously, which means your callback is called on the same thread (with a taller stack), which means you better not schedule another async call in a loop.)

like image 132
Brian Avatar answered Sep 17 '22 18:09

Brian