2012-12-17 26 views
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; 
    } 
    } 
    } 
+7

使用'隊列'而不是列表。 – Mir

+1

如果您希望它更安全,可以使用'ConcurrentQueue'或'BlockingCollection'。 – Servy

+0

非常感謝你們的回覆我還在上面添加了我的代碼,請看看這個 – user1465977

回答

2
  • 對於FIFO應使用Queue<T>
  • 對於LIFO,您應該使用Stack<T>。有關集合等

更多信息,如果您使用的.NET 4中找到here

以上你可以使用ConcurrentQueue<T>,閱讀更多here。 如果您使用的是.NET 3.5或更低版本您可以使用同步隊列爲線程安全,一個很好的例子可以找到here

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. 
*/ 
+0

這應該是一個評論,除非你打算詳細說明。 – Servy

+0

'Stack'不是隊列。 「隊列」是一個隊列。堆棧也不比隊列「更普遍」。沒有跡象表明,考慮到他的要求,認爲他需要一個堆棧。 – Servy

+0

非常感謝你們的回覆我正在試圖用這種方式做,但我不確定它是否正確或不。 – user1465977

0

它幾乎聽起來像你只是想使用IEnumerable。這是通過只讀界面讀取列表中所有項目的方法。您可以在LINQ中使用Concat以獲得代表這兩個列表中項目的IEnumerable

但是,IEnumerable不是線程安全的,所以如果您打算從多個線程訪問隊列,那麼您將需要使用BlockingCollection,默認情況下它會包裝ConcurrentQueue。然後,您可以將這兩個列表中的所有項目添加到隊列中,並從任意數量的線程中讀取它。