Firstly, i’ll explain a short scenario;
As a signal from certain devices triggers, an object of type Alarm is added to a queue. At an interval, the queue is checked, and for each Alarm in the queue, it fires a method.
However, the problem i’m running into is that, if an alarm is added to the queue whilst it’s being traversed, it throws an error to say that the queue has changed whilst you were using it. Here’s a bit of code to show my queue, just assume that alarms are being constantly inserted into it;
public class AlarmQueueManager
{
public ConcurrentQueue<Alarm> alarmQueue = new ConcurrentQueue<Alarm>();
System.Timers.Timer timer;
public AlarmQueueManager()
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DeQueueAlarm();
}
private void DeQueueAlarm()
{
try
{
foreach (Alarm alarm in alarmQueue)
{
SendAlarm(alarm);
alarmQueue.TryDequeue();
//having some trouble here with TryDequeue..
}
}
catch
{
}
}
So my question is, how do i make this more…thread safe? So that i won’t run into these issues. Perhaps something along the lines of, copying the queue to another queue, working on that one, then dequeueing the alarms that were dealt with from the original queue?
edit: just been informed of concurrent queue, will check this out now
Alternatively, you could use:
Per the MSDN article on
ConcurrentQueue<T>.GetEnumerator:Thus, the difference between the two approaches arises when your
DeQueueAlarmmethod is called concurrently by multiple threads. Using theTryQueueapproach, you are guaranteed that eachAlarmin the queue would only get processed once; however, which thread picks which alarm is determined non-deterministically. Theforeachapproach ensures that each racing thread will process all alarms in the queue (as of the point in time when it started iterating over them), resulting in the same alarm being processed multiple times.If you want to process each alarm exactly once, and subsequently remove it from the queue, you should use the first approach.