Skip to content

Instantly share code, notes, and snippets.

@JefStat
Last active August 29, 2015 14:07
Show Gist options
  • Save JefStat/3da7ea8f5cc00d246f0f to your computer and use it in GitHub Desktop.
Save JefStat/3da7ea8f5cc00d246f0f to your computer and use it in GitHub Desktop.
Queue consumer
public class PollingService
{
private Thread _workerThread;
private AutoResetEvent _finished;
private const int _timeout = 60*1000;
private readonly Queue<Work> _queue = new Queue<Work>();
public void StartPolling()
{
_workerThread = new Thread(Poll);
_finished = new AutoResetEvent(false);
_workerThread.Start();
}
private void Poll()
{
while (!_finished.WaitOne(_timeout) || _queue.Any())
{
var work = _queue.Pop()
work.Execute()
//do the task
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment