Created
May 22, 2014 13:53
-
-
Save mcintyre321/8d8adc71515cd2cdd4f6 to your computer and use it in GitHub Desktop.
ExecutionQueue
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe should use .FromResult instead of RunSync?