C# 在不同的事件处理程序中声明队列的位置

C# 在不同的事件处理程序中声明队列的位置,c#,winforms,queue,private,C#,Winforms,Queue,Private,您好,我正在尝试使用我的程序中的队列功能进行一个迷你游戏,该游戏接收来自加速计的X、Y、Z加速度 但是,我不知道应该在哪里或如何声明队列,以便在两个单独的事件处理程序中访问它 如您所见,我尝试了多次尝试,最后一次尝试是在两个私有事件处理程序中声明它 感谢您的帮助。谢谢 以下是我当前的代码: public Form1() { InitializeComponent(); ConnectedComPortUpdate(

您好,我正在尝试使用我的程序中的队列功能进行一个迷你游戏,该游戏接收来自加速计的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;
                    }
                }
            }
        }
    }
}
public Form1()
{
初始化组件();
ConnectedComPortUpdate();
serialPort1.DataReceived+=DataReceivedHandler;
comboBox1.DropDown+=comboBox1下拉菜单;
}
private void comboBox1下拉列表(对象发送方,事件参数e)
{
ConnectedComPortUpdate();
}
私有无效时钟刻度(对象发送方,事件参数e)
{
int xAccel;
国际亚塞尔;
扎塞尔国际酒店;
队列myXQueue=新队列();
队列myYQueue=新队列();
队列myZQueue=新队列();
while(myXQueue.Count!=0&&myYQueue.Count!=0&&myZQueue.Count!=0);
{
xAccel=myXQueue.Dequeue();
yAccel=myYQueue.Dequeue();
zAccel=myZQueue.Dequeue();
this.BeginInvoke(新事件处理程序(委托
{
XAccel.Text=XAccel.ToString(“000”);
YAccel.Text=YAccel.ToString(“000”);
ZAccel.Text=ZAccel.ToString(“000”);
}));
}
}
私有void ConnectedComPortUpdate()
{
//清除COM列表
comboBox1.Items.Clear();
//访问系统端口信息并将其添加到组合框中
comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames().ToArray());
//选择最后一个和“第一个”设备
尝试
{
comboBox1.SelectedIndex=0;
}
捕获(ArgumentOutOfRangeException)
{
显示(“请插上你的小棍子”);
comboBox1.Text=(“”);
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(!serialPort1.IsOpen)
{
尝试
{
serialPort1.PortName=comboBox1.Text;
serialPort1.Open();
comboBox1.Enabled=false;
butPortState.Text=“断开”;
Show(String.Format(“您选择了端口“{0}”,serialPort1.PortName));
}
抓住
{
MessageBox.Show(“请从下拉列表中选择一个串行端口”);
}
}
其他的
{
if(serialPort1.IsOpen)
{
serialPort1.Close();
comboBox1.Enabled=true;
butPortState.Text=“连接”;
}
}
}
私有void DataReceivedHandler(对象发送方,SerialDataReceivedEventArgs e)
{
int currentDataByte=0;
int byteToRead;
int-xAccel=0;
int-yAccel=0;
int-zAccel=0;
队列myXQueue=新队列();
队列myYQueue=新队列();
队列myZQueue=新队列();
while(serialPort1.IsOpen&&serialPort1.BytesToRead!=0)
{
尝试
{
byteToRead=serialPort1.ReadByte();
}
抓住
{
byteToRead=0;
}
if(byteToRead==255)
{
currentDataByte=0;
}
其他的
{
currentDataByte++;
开关(currentDataByte)
{
案例1:
myXQueue.Enqueue(byteToRead);
xAccel=byteToRead;
打破
案例2:
myYQueue.Enqueue(byteToRead);
yAccel=byteToRead;
打破
案例3:
myZQueue.Enqueue(byteToRead);
zAccel=byteToRead;
打破
}
}
}
}
}
}

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

// 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;
//现在可以在所有事件处理程序中使用这些。。。
队列myXQueue=新队列();
队列myYQueue=新队列();
队列myZQueue=新队列();
公共表格1()
{
初始化组件();
ConnectedComPortUpdate();
serialPort1.DataReceived+=DataReceivedHandler;
comboBox1.DropDown+=comboBox1下拉菜单;
}
private void comboBox1下拉列表(对象发送方,事件参数e)
{
ConnectedComPortUpdate();
}
私有无效时钟刻度(对象发送方,事件参数e)
{
int xAccel;
国际亚塞尔;
扎塞尔国际酒店;

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

// 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;
//现在可以在所有事件处理程序中使用这些。。。
队列myXQueue=新队列();
队列myYQueue=新队列();
队列myZQueue=新队列();
公共表格1()
{
初始化组件();
ConnectedComPortUpdate();
serialPort1.DataReceived+=DataReceivedHandler;
comboBox1.DropDown+=comboBox1下拉菜单;
}
private void comboBox1下拉列表(对象发送方,事件参数e)
{
ConnectedComPortUpdate();
}
私有无效时钟刻度(对象发送方,事件参数e)
{
int xAccel;
国际亚塞尔;
扎塞尔国际酒店;