2013-10-04 32 views
0

嗨我正在尝试在我的程序中使用队列函数,用于从加速计接收X,Y,Z加速的迷你游戏。在单独的事件处理程序中声明队列

但是我不知道应该在哪里,或者应该如何声明队列以使它在两个单独的事件处理程序中可访问。

正如你所看到的,我尝试了多次尝试,并在私人事件处理程序中声明它是我的最后一次尝试。

任何帮助表示赞赏。谢谢!

这里是我当前的代码:

 public Form1() 
     { 
      InitializeComponent(); 

      ConnectedComPortUpdate(); 
      serialPort1.DataReceived += DataReceivedHandler; 

      comboBox1.DropDown += comboBox1_DropDown; 

     } 

     private void comboBox1_DropDown(object sender, EventArgs e) 
     { 
      ConnectedComPortUpdate(); 
     } 

     private void Clock_Tick(object sender, EventArgs e) 
     { 
      int xAccel; 
      int yAccel; 
      int zAccel; 

      Queue<int> myXQueue = new Queue<int>(); 
      Queue<int> myYQueue = new Queue<int>(); 
      Queue<int> myZQueue = new Queue<int>(); 

      while(myXQueue.Count!=0 && myYQueue.Count!=0 && myZQueue.Count!=0); 
      { 
       xAccel = myXQueue.Dequeue(); 
       yAccel = myYQueue.Dequeue(); 
       zAccel = myZQueue.Dequeue(); 

       this.BeginInvoke(new EventHandler(delegate 
           { 
            XAccel.Text = xAccel.ToString("000"); 
            YAccel.Text = yAccel.ToString("000"); 
            ZAccel.Text = zAccel.ToString("000"); 
           })); 

      } 

     } 

     private void ConnectedComPortUpdate() 
     { 
      //Clears COM List 
      comboBox1.Items.Clear(); 
      //Accesses System Port Information and Adds it to the ComboBox 
      comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames().ToArray()); 
      //Selects the last and "first" device 
      try 
      { 
       comboBox1.SelectedIndex = 0; 
      } 
      catch (ArgumentOutOfRangeException) 
      { 
       MessageBox.Show("Please plug in your tiny stick"); 
       comboBox1.Text = (" "); 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (!serialPort1.IsOpen) 
      { 
       try 
       { 
        serialPort1.PortName = comboBox1.Text; 
        serialPort1.Open(); 
        comboBox1.Enabled = false; 
        butPortState.Text = "Disconnect"; 
        MessageBox.Show(String.Format("You selected port '{0}'", serialPort1.PortName)); 
       } 
       catch 
       { 
        MessageBox.Show("Please select a serial port from the drop down list"); 
       } 
      } 
      else 
      { 
       if (serialPort1.IsOpen) 
       { 
        serialPort1.Close(); 
        comboBox1.Enabled = true; 
        butPortState.Text = "Connect"; 
       } 
      } 
     } 

     private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) 
     { 
      int currentDataByte = 0; 
      int byteToRead; 
      int xAccel = 0; 
      int yAccel = 0; 
      int zAccel = 0; 

      Queue<int> myXQueue = new Queue<int>(); 
      Queue<int> myYQueue = new Queue<int>(); 
      Queue<int> myZQueue = new Queue<int>(); 

      while (serialPort1.IsOpen && serialPort1.BytesToRead != 0) 
      { 
       try 
       { 
        byteToRead = serialPort1.ReadByte(); 
       } 
       catch 
       { 
        byteToRead = 0; 
       } 
       if (byteToRead == 255) 
       { 
        currentDataByte = 0; 
       } 
       else 
       { 
        currentDataByte++; 
        switch (currentDataByte) 
        { 
         case 1: 
          myXQueue.Enqueue(byteToRead); 
          xAccel = byteToRead; 
          break; 
         case 2: 
          myYQueue.Enqueue(byteToRead); 
          yAccel = byteToRead; 
          break; 
         case 3: 
          myZQueue.Enqueue(byteToRead); 
          zAccel = byteToRead; 
          break; 
        } 
       } 
      } 
     } 
    } 
} 
+1

请使用标签平台(WP8, Win8?) –

+0

“这个放哪里?” - 当然不会在Form1的代码后面。 –

+0

为什么没有在Form1中?对不起,我是C#的初学者。 – user2847580

回答

3

您需要在类/实例级别声明队列:

// These can now be used in all event handlers... 
Queue<int> myXQueue = new Queue<int>(); 
Queue<int> myYQueue = new Queue<int>(); 
Queue<int> myZQueue = new Queue<int>(); 

public Form1() 
{ 
    InitializeComponent(); 

    ConnectedComPortUpdate(); 
    serialPort1.DataReceived += DataReceivedHandler; 

    comboBox1.DropDown += comboBox1_DropDown; 

} 

private void comboBox1_DropDown(object sender, EventArgs e) 
{ 
    ConnectedComPortUpdate(); 
} 

private void Clock_Tick(object sender, EventArgs e) 
{ 
    int xAccel; 
    int yAccel; 
    int zAccel; 
+0

非常感谢!有效! – user2847580

1

我不认为这个问题的答案(或即使这个问题本身)也必然是C#特有的。考虑一个问题“我应该在哪里声明变量X”答案几乎总是“在可能的最窄范围内,每个需要使用变量X的地方都可以访问”

对于您的情况,答案可能是“在课堂上“

或者,如果您要以更实用的风格进行编程,答案可能是”重新考虑程序的结构,以便X可以作为参数传递给需要它的函数“。大多数C#事件处理程序都有一个可以粘贴“用户状态”对象的地方,以便它可以从事件源传递到事件处理程序。

答案将是C,C++,Java等 相同的(也许这应该是一个评论,但恐怕这是一个有点长)