Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created June 13, 2013 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendettamit/5774499 to your computer and use it in GitHub Desktop.
Save vendettamit/5774499 to your computer and use it in GitHub Desktop.
Async delegate template using tasks
/// <summary>
/// DoAsync is a method template to perform any task asynchronously.
/// </summary>
/// <typeparam name="T">type of argument</typeparam>
/// <param name="action">method</param>
/// <param name="argument">value of argument of type T</param>
public static void DoAsync<T>(Action<T> action, T argument)
{
// Invoke the task to execute the item
Task.Factory.StartNew(() => action.Invoke(argument),
TaskCreationOptions.PreferFairness).ContinueWith(
(parameter) =>
{
if (parameter.Exception != null)
{
throw parameter.Exception.Flatten();
}
},
TaskContinuationOptions.OnlyOnFaulted).ContinueWith(
(parameter) =>
{
// this.Logger.Info("Command successfully executed"),
},
TaskContinuationOptions.NotOnFaulted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment