Created
June 13, 2013 15:13
-
-
Save vendettamit/5774499 to your computer and use it in GitHub Desktop.
Async delegate template using tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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