Skip to content

Instantly share code, notes, and snippets.

@mcintyre321
Created May 22, 2014 13:53
Show Gist options
  • Select an option

  • Save mcintyre321/8d8adc71515cd2cdd4f6 to your computer and use it in GitHub Desktop.

Select an option

Save mcintyre321/8d8adc71515cd2cdd4f6 to your computer and use it in GitHub Desktop.
ExecutionQueue
public class ExecutionQueue
{
BlockingCollection<Action> bc = new BlockingCollection<Action>();
public ExecutionQueue(){
Task.Factory.StartNew(() =>
{
foreach (Action value in bc.GetConsumingEnumerable())
{
value();
}
});
}
public Task<T> Enqueue<T>(Func<T> func){
T result = default(T);
var task = new Task<T>(() => {
return result;
});
bc.Add(() => {
result = func();
task.RunSynchronously();
});
return task;
}
public Task Enqueue(Action func){
var task = new Task(() => {
});
bc.Add(() => {
func();
task.RunSynchronously();
});
return task;
}
}
@mcintyre321
Copy link
Copy Markdown
Author

Maybe should use .FromResult instead of RunSync?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment