I've implemented a work queue based on something I found here > Task queue for wp8? ...but am having trouble implementing additional functionality with it.
I'd taken out the Func<Task>
's and replaced them with ICommands (holding their own CancellationTokens), and intended to add Pause()
, Resume()
, Save()
& Restore()
methods. This is so OnFormClose()
I can pause the queue processing and prompt the user to decide whether he wants to "wait for the queue to finish" (i.e. resume), or "exit now" (i.e. save and exit).
public class WqController
{
private readonly Queue<ICommand> _queue = new Queue<ICommand>();
private Task _queueProcessor;
private ICommand _curCommand;
public void Enqueue(ICommand command)
{
_queue.Enqueue(command);
if (_queueProcessor == null) _queueProcessor = ProcessQueue();
}
private async Task ProcessQueue()
{
try
{
while (_queue.Count != 0)
{
_curCommand = _queue.Peek();
try
{
await Task.Run(() => _curCommand.Execute());
}
catch (OperationCanceledException)
{
Console.WriteLine("QUEUE PAUSED");
return;
}
catch (Exception)
{
Console.WriteLine("FAILED TO EXECUTE COMMAND");
}
_queue.Dequeue();
}
}
finally
{
_queueProcessor = null;
_curCommand = null;
}
}
public async Task Cancel()
{
_curCommand.Cts.Cancel(true);
await _queueProcessor;
}
public void Resume()
{
_queueProcessor = ProcessQueue();
}
}
The Save()
& Restore()
work fine, so I haven't included them here. The Cancel()
works intermittently / unreliably, and the Restore()
doesn't seem to work at all (confusingly to me, as I'm basically attempting just the same restart as works in the Enqueue()
method).