没有使用委托多线程修复读取,并且不会弹出任何表单

时间:2012-06-12 15:46:20

标签: c# .net delegates serial-port

我需要使用从串口接收的字符串更新Windows窗体标签。我的代码已经存在两个问题。

因为读取串口需要另一个线程,所以我使用委托方法来更新标签文本。

第一个问题是表单窗口在我启动程序时不会打开(当我在initSerialPort()中没有调用Form1_Load()时它会打开。)

第二个问题是,当我在Debug.Write(message)中呼叫_self.SetText(message)时似乎无法触及Read()。当我注释掉_self.SetText(message)时,它会记录消息,但也不会打开表单窗口,因为在initSerialPort()中调用了Form1_Load()

我有点像C#的菜鸟,只是你知道;)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        delegate void SetTextCallback(string text);

        private static SerialPort _serialPort;
        private static Boolean _continue;
        private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        private static Thread readThread = new Thread(Read);
        private static Form1 _self;
        private static Label _lbl;

        public Form1()
        {
            InitializeComponent();
            _self = this;

            _lbl = label1;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initSerialPort();
        }

        public void setMessage(string mes)
        {
            label1.Text = mes;
        }

        private static void initSerialPort()
        {
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.Open();
            _continue = true;
            readThread.Start();

            readThread.Join();
            _serialPort.Close();
            _serialPort = null;
        }

        public static void Read()
        {
            Debug.Write("testread");
            while (_continue)
            {

                try
                {
                    String message = _serialPort.ReadLine();

                    _self.SetText(message);

                    Debug.Write(message);


                }
                catch (TimeoutException) { }

            }
        }

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.label1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.label1.Text = text;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要Join到您创建的新主题。这将阻止线程,直到您的Read方法完成,这意味着它永远不会完成。