如何在按下按钮一次后让代码一遍又一遍地运行而不需要 while 循环(C#)[关闭]

问题描述 投票:0回答:1

我是 C# 新手,正在创建一个遥测仪表板,其中一个按钮可打开串行监视器。当我按下打开它的按钮时,我想要一个循环,它不会像一会儿那样冻结其他按钮。

我尝试过使用单独的线程,但要写入文本框和图表的表单,我需要代码与主循环位于同一线程中。有没有一种方法,当我按下按钮时,每次代码“循环”通过时都会出现写入图表的代码?我尝试过使用计时器,但还没有成功。下面我将链接代码(抱歉,额外的图表_单击它不会让我无错误地删除)

namespace dashboard_telemetry
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
            Text = "Dashboard ver. 1";
        }
        
        /// <summary>
        /// Open Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static System.Timers.Timer aTimer;
        private void button1_Click(object sender, EventArgs e)
        {
            button_open.Text = "Opened";
            button_open.Enabled = true;
            button_close.Enabled = false;
            try
            {
                serialPort1.PortName = comboBox_port.Text;
                serialPort1.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// Upon loading the Form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox_port.Items.Add(port);
            }
            button_close.Enabled = false;
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            button_open.Enabled = false;
            button_close.Enabled = true;
            serialPort1.Close();

        }

        private void textBox_Recieve_TextChanged(object sender, EventArgs e)
        {

        }
         
        private void button1_Click_1(object sender, EventArgs e)
        {
// button1_1 is the button that starts logging data to charts
            serialPort1.BaudRate = Convert.ToInt32(comboBoxbaud.Text);
             if (serialPort1.IsOpen)
                    {
                        //This code will only run if I click the button  
                        // but I need it to run over and over 
                        string newPacket = serialPort1.ReadLine();
                        string[] data = newPacket.Split(',');
                        tBoxDataIN.Text = newPacket;
                        chart1.Series["Gyro X"].Points.AddXY(data[1], data[0]);
        }
       
    private void chart7_Click(object sender, EventArgs e)
        {

        }

        private void label_port_Click(object sender, EventArgs e)
        {

        }

        private void comboBox_port_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(100);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void chart2_Click(object sender, EventArgs e)
        {

        }

        private void chart4_Click(object sender, EventArgs e)
        {

        }
    }
}

c#
1个回答
1
投票

我只能给你这么多帮助,因为问题有点模糊。

private bool showSerialData = false;    // Use Variables to store state, you don't want to use Control Properties,   
// unless the data is attached to the control. Even then most  
// people would create a custom control that derives from the base control with their  
// own properties.

void Main() {
  while (true)    // This will freeze the UI thread,  I'm demo'ing a loop
  {
    if (showSerialData) //  == true)
    {
        // write serial data to charts
    }
    // TODO detect another variable or key stroke to exit.
  }
}

private void button_open_click(object sender, EventArgs e)
{
    // Switchs on or off, the opposite of its last state
    showSerialData = !showSerialData;
}
© www.soinside.com 2019 - 2024. All rights reserved.