Last active
August 29, 2015 14:07
-
-
Save JefStat/3da7ea8f5cc00d246f0f to your computer and use it in GitHub Desktop.
Queue consumer
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 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