Esempio n. 1
0
 private void Window_Loaded_1(object sender, RoutedEventArgs e)
 {
     BarCtr.DrawBackground();
     CurveCtr.DrawBackground();
     timer = new Thread(new ThreadStart(() =>
     {
         Random rd = new Random();
         while (true)
         {
             try
             {
                 Dispatcher.Invoke(new Action(() =>
                 {
                     double d = rd.Next(20, 30);
                     BarCtr.DrawLine(d);
                     CurveCtr.DrawLine(d);
                 }));
             }
             catch (Exception)
             {
             }
             Thread.Sleep(700);
         }
     }));
     timer.Start();
 }
Esempio n. 2
0
        Thread timer;//声明线程变量

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //获取本机所有串口的名字
            string[] strPortName = SerialPort.GetPortNames();
            //将本机所有串口名称复制给cmbPort控件
            cmbPort.ItemsSource = strPortName;

            //下拉框初始化
            //如果本机串口数量不为0,则将cmbPort设为Item的第一个索引
            if (strPortName.Length > 0)
            {
                cmbPort.SelectedIndex = 0;
            }
            //将波特率下拉框cmbBaudRate的设为Item的第一个索引
            cmbBaudRate.SelectedIndex = 0;
            //将数据位下拉框cmbBaudRate的设为Item的第一个索引
            cmbDataBits.SelectedIndex = 0;
            //将停止位下拉框cmbBaudRate的设为Item的第一个索引
            cmbStopBits.SelectedIndex = 0;
            //将奇偶校验位下拉框cmbBaudRate的设为Item的第一个索引
            cmbParity.SelectedIndex = 0;

            //绘制背景
            CurveCtr.DrawBackground();
            BarCtr.DrawBackground();
            timer = new Thread(new ThreadStart(() =>//线程实例化
            {
                while (true)
                {
                    try
                    {
                        Dispatcher.Invoke(new Action(() =>
                        {
                            CurveCtr.DrawLine(temp); //绘制温度折线
                            BarCtr.DrawLine(hum);    //绘制湿度直方图
                        }));
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(700);
                }
            }));
            timer.Start();//开启线程
        }