为什么ConcurrentQueue.Enqueue方法没有添加值?

时间:2016-10-05 07:49:11

标签: c# wpf concurrent-collections

我将ConcurrentQueue定义为WPF MVVM应用程序中的ViewModel类字段:

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

下面是AGC_DataRecordToSave定义:

/// <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
}

然后在System.Windows.Threading.DispatcherTimer tick处理程序(在同一个类中)中执行以下操作:

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);

_agcAbsoluteDataRecordsToSaveBuf保持为空(Count == 0)。因此ConcurrentQueue.Enqueue方法不会将值添加到集合中。创建 AGC_DataRecordToSave记录对象并成功填充数据。我在debuger检查它。为什么不将值添加到集合中?为什么会出现这种情况呢?请帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

我问OP:谁将这个队列中的项目出列?,并且OP回答:同一类中的方法。此方法位于并行TPL任务内。

因此,在检查队列计数的时候,一些并行任务已经排队了队列项目!