0

I define ConcurrentQueue as a ViewModel class field in my WPF MVVM application:

private ConcurrentQueue<AGC_DataRecordToSave> _agcAbsoluteDataRecordsToSaveBuf = new ConcurrentQueue<AGC_DataRecordToSave>();

Below is AGC_DataRecordToSave definition:

/// <summary>
/// Structure of record to write in CSV-file.
/// </summary>
public class AGC_DataRecordToSave
{
    #region Public Fields

    /// <summary>
    /// Date
    /// </summary>
    public DateTime Data;
    /// <summary>
    /// Time
    /// </summary>
    public TimeSpan Time;
    /// <summary>
    /// Measuring Ultrasonic Beam.
    /// </summary>
    public int MeasuringBeam;
    /// <summary>
    /// Direction.
    /// </summary>
    public string Direction;
    /// <summary>
    /// Value from outer device.
    /// </summary>
    public double Value;

    #endregion
}

Then in System.Windows.Threading.DispatcherTimer tick handler (in the same class) I do the following:

AGC_DataRecordToSave record = new AGC_DataRecordToSave();
record.Data = new DateTime(this._currentDate.Date.Ticks);
record.Time = this._currentDate.TimeOfDay;
record.MeasuringBeam = 1;
record.Direction = "P1AB";
record.Value = automaticGainControl;
this._agcAbsoluteDataRecordsToSaveBuf.Enqueue(record);

And _agcAbsoluteDataRecordsToSaveBuf remains empty (Count == 0). So ConcurrentQueue.Enqueue method doesn't add the value to the collection. AGC_DataRecordToSave record object is created and filled with the data successfully. I check it in debuger. Why the value is not added to collection? Why this situation has plase? Please help to solve this problem.

Prohor
  • 141
  • 1
  • 3
  • 12

1 Answers1

1

I asked the OP: Who dequeues items on this queue?, and OP answered: Method in same class. This method is inside parallel TPL Task..

So for the time that you check queue's count, some of the parallel tasks have already dequeued the queued item!

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206