I am looking for a simple and efficient way to asynchronously (wait / block) without having to poll within an Async Task method.
I have created a simple psuedo-code situation below:
private var queue = new ConcurrentQueue<Command>();
// Function called by program
public async Task<String> GetNameAsync(int id)
{
//Build Command
var Command = new Command(id);
//Enqueue Command to concurrent queue
queue.enqueue(command);
//Wait for command to finnish executing, posibly supply a timeout
await command.CompleteCondition
//Once command has completed or timed out return the result of the task.
return command.Response.getString();
}
//Continiously runs in its own thread
private void MainThread()
{
while(mustRun)
{
if(queue.Any())
{
//Get command from queue if there is one
var command = queue.dequeue();
//Execute command
var result = ExecuteCcommand(command);
//Set Command Done with result (so the blocking async task can continue:
//TODO:
}else{
Thread.Sleep(100);
}
}
I have left out the mechanism which I do not know of, But essentially I need to pass some sort of lock along with the command to the main thread which will then notify the Async Task once it has completed, so that the task can continue.
I am sure there must me some type of c# mechanism out there which is specifically designed to be used with the c# Async Task library. I am obviously not aware of it and have never worked with it. What would you recommend I use?
TaskCompletionSource- it gives you aTaskthat you can set as you want (e.g.SetResult/SetException/SetCancelled. Alternatively, you can implement your own awaitable (simply a class/struct that has a method namedGetAwaiter:D). Another option would be to turn your "continously running on its own thread" worker into aTaskScheduler, and simply start a task on that scheduler. There's plenty of options, really :)ConcurrentQueuewith aBlockingCollection(which has a queue as the underlying storage by default), you can replace your producer with a fully blockingBlockingCollection.GetConsumingEnumerable- no need for awhile (true)orThread.Sleep.