box.StateMachine = stateMachine;
box.Context = currentContext;
- // Finally, log the creation of the state machine box object / task for this async method.
+ // Log the creation of the state machine box object / task for this async method.
if (AsyncCausalityTracer.LoggingOn)
{
AsyncCausalityTracer.TraceOperationCreation(box, "Async: " + stateMachine.GetType().Name);
}
+ // And if async debugging is enabled, track the task.
+ if (System.Threading.Tasks.Task.s_asyncDebuggingEnabled)
+ {
+ System.Threading.Tasks.Task.AddToActiveTasks(box);
+ }
+
return box;
}
if (IsCompleted)
{
+ // If async debugging is enabled, remove the task from tracking.
+ if (System.Threading.Tasks.Task.s_asyncDebuggingEnabled)
+ {
+ System.Threading.Tasks.Task.RemoveFromActiveTasks(this);
+ }
+
// Clear out state now that the async method has completed.
// This avoids keeping arbitrary state referenced by lifted locals
// if this Task / state machine box is held onto.
{
Debug.Assert(m_task != null, "Expected non-null task");
- if (AsyncCausalityTracer.LoggingOn || System.Threading.Tasks.Task.s_asyncDebuggingEnabled)
- {
- LogExistingTaskCompletion();
- }
-
- if (!m_task.TrySetResult(result))
- {
- ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted);
- }
- }
-
- /// <summary>Handles logging for the successful completion of an operation.</summary>
- private void LogExistingTaskCompletion()
- {
- Debug.Assert(m_task != null);
-
if (AsyncCausalityTracer.LoggingOn)
{
AsyncCausalityTracer.TraceOperationCompletion(m_task, AsyncCausalityStatus.Completed);
}
- // only log if we have a real task that was previously created
- if (System.Threading.Tasks.Task.s_asyncDebuggingEnabled)
+ if (!m_task.TrySetResult(result))
{
- System.Threading.Tasks.Task.RemoveFromActiveTasks(m_task);
+ ThrowHelper.ThrowInvalidOperationException(ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted);
}
}