Skip to content

Instantly share code, notes, and snippets.

@skoon
Last active August 29, 2015 14:09
Show Gist options
  • Save skoon/62a720a213f87871a6ca to your computer and use it in GitHub Desktop.
Save skoon/62a720a213f87871a6ca to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Punchclock;
namespace AsyncPlayground
{
class Program
{
public static readonly OperationQueue queue = new OperationQueue();
static void Main(string[] args)
{
var asyncCls = new ASyncClass();
for (int i = 0; i < 4; i++)
{
asyncCls.DoTasks(i);
}
Console.ReadKey();
}
}
public class ASyncClass
{
public void DoTasks(int i)
{
MainTask(i);
}
public async Task MainTask(int i)
{
string result = string.Empty;
if (i % 2 == 0)
{
result = await Program.queue.Enqueue(1, "one", () => SubTaskOne());
}
else
{
result = await Program.queue.Enqueue(1, "two", () => SubTaskTwo());
}
Console.WriteLine(result);
}
public async Task<string> SubTaskOne()
{
Console.WriteLine("SubTask One");
Thread.Sleep(1);
return "SubTask One complete";
}
public async Task<string> SubTaskTwo()
{
Console.WriteLine("SubTask Two");
Thread.Sleep(100);
return "SubTask Two complete";
}
//if you change the Thread.Sleep calls to await Task.Delay() calls, the OperationQueue performs as expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment