protected virtual void Complete(IntPtr userToken)
{
bool offloaded = false;
ThreadContext threadContext = CurrentThreadContext;
try
{
++threadContext._nestedIOCount;
if (_asyncCallback != null)
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Invoking callback");
if (threadContext._nestedIOCount >= ForceAsyncCount)
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "*** OFFLOADED the user callback ****");
Task.Factory.StartNew(
s => WorkerThreadComplete(s),
this,
CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default);
offloaded = true;
}
else
{
_asyncCallback(this);
}
}
else
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "No callback to invoke");
}
}
finally
{
--threadContext._nestedIOCount;
// Never call this method unless interlocked _intCompleted check has succeeded (like in this case).
if (!offloaded)
{
Cleanup();
}
}
}