0

我想为列表中的所有项目创建一个 FIFO 队列。我需要从队列中检索列表中的项目。

这是我的代码:

using System.Collections.Generic;

namespace CHPCanControl
{
  public class signals
 {
    public double quant;
    public int Ind;
    public int Subin;
    public int Control;
    public int dat;
    public List<signals> signalList;

    public signals(double quant, int Ind, int Subin, int Control, int dat)
    {

        this.quant = quant;
        this.Ind = Ind;
        this.Control = Control;
        this.Subin = Subin;
        this.dat = dat;
    }
 }

 public class Controlsignal 
{
   public List<signals> signalList = new List<signals>(); // this is my first list

    public void main()
    {
        signalList.Add(new signals(1, 1000, 1, 0x60, 1));
        signalList.Add(new signals(1, 1000, 2, 0x60, 1));
        signalList.Add(new signals(1, 1000, 3, 0x60, 1));
        signalList.Add(new signals(1, 1000, 4, 0x60, 1));
        signalList.Add(new signals(0.5, 1000, 5, 0x60, 200));
        signalList.Add(new signals(1, 1000, 6, 0x60, 1));
        signalList.Add(new signals(1, 1000, 7, 0x60, 1));
        signalList.Add(new signals(0.0625, 1000, 8, 0x60, 1));
        signalList.Add(new signals(0.1, 1000, 9, 0x60, 1));
        signalList.Add(new signals(0.1, 1000, 10, 0x60, 1));
    }
  }

   public class Statussignal
{
   public List<signals> signalList1 = new List<signals>(); // this is my second list

    public void main()
    {
        signalList1.Add(new signals(1, 1100, 1, 0x40, 1));
        signalList1.Add(new signals(1, 1100, 2, 0x40, 1));
        signalList1.Add(new signals(1, 1100, 3, 0x40, 1));
        signalList1.Add(new signals(0.0002, 1100, 4, 0x40, 1));
        signalList1.Add(new signals(0.5, 1100, 5, 0x40, 200));
        signalList1.Add(new signals(1, 1100, 6, 0x40, 1));
        signalList1.Add(new signals(1, 1100, 7, 0x40, 1));
        signalList1.Add(new signals(1, 1100, 8, 0x40, 1));
    }
  }
 }

我的问题是我想将我的两个列表中的项目添加到 FIFO 队列中。我在想我可能需要从多个线程访问队列。

非常感谢你们的回复,我试图用这种方式做,但我不确定它是否正确..

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Windows.Forms;
  using System.Text.RegularExpressions;
  using System.Threading;


 namespace CHPCanControl
{
public partial class Form1 : Form
{

    Controlsignal control = new Controlsignal();
    Statussignal status = new Statussignal();
    Calibrationparameters calibration = new Calibrationparameters();


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        // need to do something here by which you will receieve 
        // your messages here from the ECU

        Thread thread = new Thread(new ThreadStart(this.ECU_Send));
        thread.Start();
        thread.IsBackground = true;

    }
    public void ECU_Send()
    {
        private Queue<control.signalList> queue = new Queue<control.signalList>();
        public event EventHandler Changed;
        protected virtual void OnChanged()
        {
            if(Changed != null)
            {
                Changed(this,EventArgs.Empty);
            }
        }
    public int Count { get { return queue.Count; } }

    public virtual void Enqueue(control.signalList item)
    {
        queue.Enqueue(item);
            OnChanged();
    }


    public virtual void Dequeue()
    {
       control.signalList item = queue.Dequeue();
        OnChanged();
        return item;
    }
   }
  }
4

2 回答 2

2
  • 对于 FIFO,您应该使用Queue<T>.
  • 对于 LIFO,您应该使用Stack<T>.

关于收藏等的更多信息可以在这里找到

如果您使用的是.NET 4 或更高版本,您可以使用,请在此处ConcurrentQueue<T>阅读更多内容。如果您使用的是.NET 3.5 或更低版本,您可以使用同步队列来确保线程安全,可以在此处找到一个很好的示例。

using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Creates a synchronized wrapper around the Queue.
      Queue mySyncdQ = Queue.Synchronized( myQ );

      // Displays the sychronization status of both Queues.
      Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" );
      Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" );
   }
}
/* 
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/
于 2012-12-17T15:22:37.217 回答
0

这几乎听起来像您只想使用IEnumerable. 这是一种通过只读界面从列表中读取所有项目的方法。您可以Concat在 LINQ 中使用来获取IEnumerable表示两个列表中的项目的一个。

但是,IEnumerable它不是线程安全的,因此如果您计划从多个线程访问队列,那么您将需要使用 a BlockingCollection,它会默认包装 a ConcurrentQueue。然后,您可以将两个列表中的所有项目添加到队列中,并从任意数量的线程中读取它。

于 2012-12-17T15:32:50.770 回答