Skip to content

Instantly share code, notes, and snippets.

@punitganshani
Created August 12, 2015 15:18
Show Gist options
  • Save punitganshani/3d51bd906fe746c2e9e8 to your computer and use it in GitHub Desktop.
Save punitganshani/3d51bd906fe746c2e9e8 to your computer and use it in GitHub Desktop.
AsyncContext for Console Applications - Static Main
public class AsyncContext
{
public static void Run(params Func<Task>[] tasks)
{
if (tasks == null)
return;
Task[] asyncTasks = new Task[tasks.Length];
int counter = 0;
foreach (var task in tasks)
{
asyncTasks[counter++] = Task.Factory.StartNew<Task>(async () => { await task(); });
}
Task.WaitAll(asyncTasks);
}
}
class Program
{
static void Main(string[] args)
{
AsyncContext.Run(DoSomethingAsync);
}
private static async Task DoSomethingAsync()
{
// async operation here
var result = await MyOperationAsync();
var workCompleted = result.GetCompleteJson();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment