private static void Stop(this IStory story, Task task) { Ensure.ArgumentNotNull(story, "story"); Ensure.ArgumentNotNull(task, "task"); story.Task = task; story.Stop(); }
/// <summary> /// Invokes the task to be observed by this story. /// </summary> /// <param name="action">Action to observe.</param> public static void Run(this IStory story, Action <IStory> action) { var tcs = new TaskCompletionSource <object>(); try { story.Start(); action(story); tcs.SetResult(null); } catch (Exception exception) { tcs.SetException(exception); throw; } finally { story.Stop(tcs.Task); } }
/// <summary> /// Invokes the task to be observed by this story. /// </summary> /// <param name="storyFactory">The story factory.</param> /// <param name="name">The name.</param> /// <param name="action">Action to observe.</param> public static void StartNew(this IStoryFactory storyFactory, string name, Action action) { Ensure.ArgumentNotNull(storyFactory, "storyFactory"); Ensure.ArgumentNotNull(action, "action"); IStory story = storyFactory.CreateStory(name); try { story.Start(); action(); } catch (Exception exception) { story.Data["exception"] = exception; throw; } finally { story.Stop(); } }
/// <summary> /// Invokes the task to be observed by this story. /// </summary> /// <typeparam name="T">The task result type.</typeparam> /// <param name="storyFactory">The story factory.</param> /// <param name="name">The name.</param> /// <param name="func">Function returning a task to observe.</param> /// <returns> /// The result. /// </returns> public static T StartNew <T>(this IStoryFactory storyFactory, string name, Func <T> func) { Ensure.ArgumentNotNull(storyFactory, "storyFactory"); Ensure.ArgumentNotNull(func, "func"); IStory story = storyFactory.CreateStory(name); try { story.Start(); return(func()); } catch (Exception exception) { story.Data["exception"] = exception; throw; } finally { story.Stop(); } }
/// <summary> /// Invokes the task to be observed by this story. /// </summary> /// <typeparam name="T">The task result type.</typeparam> /// <param name="func">Function returning a task to observe.</param> /// <returns>The result.</returns> public static T Run <T>(this IStory story, Func <IStory, T> func) { var tcs = new TaskCompletionSource <T>(); try { story.Start(); var result = func(story); tcs.SetResult(result); return(result); } catch (Exception exception) { tcs.SetException(exception); throw; } finally { story.Stop(tcs.Task); } }