public Task <T> Run <T>(IActivity <T> level) { var oldLevel = activityStack.LastOrDefault(); if (oldLevel != null) { using (oldLevel.Activate()) oldLevel.Level.Deactivated(); } currentActivity = new ActivityRuntime(level); activityStack.Push(currentActivity); using (currentActivity.Activate()) { level.Started(); level.Activated(); return(level.Run().ContinueWith(t => { using (currentActivity.Activate()) { level.Deactivated(); level.Completed(); } currentActivity = oldLevel; if (oldLevel != null) { using (oldLevel.Activate()) oldLevel.Level.Activated(); } return t.Result; }, TaskScheduler.FromCurrentSynchronizationContext())); } }