[英]Wait for thread to finish without blocking UI thread
我正在編寫winForm
應用程序,它必須列出SerialPort
並將數據實時打印到多行textbox
。
這是代碼:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
namespace SCPListener
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
GetAvailablePortNames();
panel1.BackColor = ColorTranslator.FromHtml("#f44147");
cbDefaultValue();
}
String[] PortNames;
bool conn = true;
String data;
public string datetime;
Form2 frm = new Form2();
void GetAvailablePortNames()
{
PortNames = SerialPort.GetPortNames();
comboBox1.Items.AddRange(PortNames);
}
void RefreshAvailablePortNames()
{
comboBox1.Items.Clear();
PortNames = SerialPort.GetPortNames();
comboBox1.Items.AddRange(PortNames);
}
public string getDataTime()
{
DateTime time = DateTime.Now;
string date = time.ToString(@"hh\:mm\:ss");
return date;
}
public void GetData()
{
string date = getDataTime();
data = serialPort1.ReadLine();
textBox1.AppendText("[" + date + "] " + "Received: " + data + "\r\n");
}
public void cbDefaultValue()
{
comboBox1.SelectedIndex = 0;
comboBox5.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "" || comboBox4.Text == "" || comboBox5.Text == "")
{
MessageBox.Show("Please port settings", "Incorrect port settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
DateTime time = DateTime.Now;
string date = time.ToString(@"hh\:mm\:ss");
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
switch (comboBox3.Text)
{
case "5":
serialPort1.DataBits = 5;
break;
case "6":
serialPort1.DataBits = 6;
break;
case "7":
serialPort1.DataBits = 7;
break;
case "8":
serialPort1.DataBits = 8;
break;
default:
serialPort1.DataBits = 5;
break;
}
switch (comboBox4.Text)
{
case "None":
serialPort1.Parity = Parity.None;
break;
case "Odd":
serialPort1.Parity = Parity.Odd;
break;
case "Even":
serialPort1.Parity = Parity.Even;
break;
case "Mark":
serialPort1.Parity = Parity.Mark;
break;
case "Space":
serialPort1.Parity = Parity.Space;
break;
}
switch (comboBox5.Text)
{
case "One":
serialPort1.StopBits = StopBits.One;
break;
case "Two":
serialPort1.StopBits = StopBits.Two;
break;
case "OnePointFive":
serialPort1.StopBits = StopBits.OnePointFive;
break;
}
serialPort1.Open();
string CurrentPornName = comboBox1.Text;
label7.Text = "Opened " + CurrentPornName;
panel1.BackColor = ColorTranslator.FromHtml("#42f477");
comboBox1.Enabled = false;
comboBox2.Enabled = false;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
//button5.Enabled = false;
//button6.Enabled = false;
}
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
MessageBox.Show("Unauthorized Acces","Access Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex is System.IO.IOException)
{
MessageBox.Show("Please plug in your device", "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
panel1.BackColor = ColorTranslator.FromHtml("#f44147");
string CurrentPornName = comboBox1.Text;
label7.Text = "Closed " + CurrentPornName; ;
serialPort1.Close();
comboBox1.Enabled = true;
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
//button5.Enabled = true;
//button6.Enabled = true;
}
private void button5_Click(object sender, EventArgs e)
{
RefreshAvailablePortNames();
}
private void button3_Click(object sender, EventArgs e)
{
//backgroundWorker1.RunWorkerAsync();
Thread thread = new Thread(start: ()=>
{
try
{
datetime = getDataTime();
string date = getDataTime();
startListening:
if (serialPort1.BytesToRead > 0)
{
datetime = getDataTime();
data = serialPort1.ReadLine();
textBox2.AppendText("[" + datetime + "] " + "Received: " + data + "\r\n");
}
else
textBox2.AppendText("[" + datetime + "] " + "Error: There is no data to read" + "\r\n");
if (conn)
goto startListening;
}
catch (Exception ex)
{
DateTime time = DateTime.Now;
string date = time.ToString(@"hh\:mm\:ss");
if (ex is TimeoutException)
{
textBox1.AppendText("[" + date + "] " + "Timuot Exception" + "\r\n");
}
else if (ex is InvalidOperationException)
{
MessageBox.Show("Plese check settings", "Error: Invalid Operation", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex is System.IO.IOException)
{
MessageBox.Show("Port opened, but device unpluged..", "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
});
thread.Start();
}
private void button4_Click(object sender, EventArgs e)
{
conn = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
datetime = getDataTime();
string date = getDataTime();
startListening:
if (serialPort1.BytesToRead > 0)
{
datetime = getDataTime();
data = serialPort1.ReadLine();
textBox2.AppendText("[" + datetime + "] " + "Received: " + data + "\r\n");
}
else
textBox2.AppendText("[" + datetime + "] " + "Error: There is no data to read" + "\r\n");
if (conn)
goto startListening;
}
catch (Exception ex)
{
DateTime time = DateTime.Now;
string date = time.ToString(@"hh\:mm\:ss");
if (ex is TimeoutException)
{
textBox1.AppendText("[" + date + "] " + "Timuot Exception" + "\r\n");
}
else if (ex is InvalidOperationException)
{
MessageBox.Show("Plese check settings", "Error: Invalid Operation", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex is System.IO.IOException)
{
MessageBox.Show("Port opened, but device unpluged..", "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
關於代碼:
comboBox1 -> where the Ports are listed
comboBox2 -> where the Baud Rates options are listed
comboBox3 -> where the Stop Bits options are listed
comboBox4 -> where the Data Bits options are listed
comboBox5 -> where the Parity options are listed
button1 -> to connect chosen port
button2 -> to disconnect port
button3 -> to start listening and print data into textBox in real time
button4 -> to stop listening
button5 -> refresh available ports
panel1-> to identify port is connected or not (if connected it is green if it is not connected it is red)
所以,我想要做的是當我點擊停止按鈕( button4
)時,程序必須停止收聽。 我正在嘗試使用backgroundWorker
來實現這個結果。 當我嘗試偵聽端口時使用此代碼(單擊開始按鈕button3
會打開小錯誤窗口並告訴請檢查設置(因此它根據我的代碼捕獲InvalidOperationException
);如果我不使用backgoundWorker
並刪除try{...} cath{...}
program start 確實顯示從端口接收到的數據但阻塞了用戶界面。任何想法都會非常有幫助。
button4_click 需要調用 backgroundWorker1.CancelAsync() 而不是設置 bool。 使用 BackgroundWorkers,您需要指定您希望它們結束然后檢查它,設置和檢查外部 bool 將不起作用。 backgroundWorker1_DoWork() 方法中的 try 塊應如下所示。 請記住,如果您重命名變量(在 UI 中完成或通過右鍵單擊變量並選擇“重命名”),您和其他人可以更輕松地為您提供幫助。 代碼前的另一個注意事項,使用 'goto' 幾乎是不可能的,幾乎總是有更好的選擇(在這種情況下是 while 循環)。
datetime = getDataTime();
string date = getDataTime();
while (!backgroundWorker1.CancellationPending)
{
if (serialPort1.BytesToRead > 0)
{
datetime = getDataTime();
data = serialPort1.ReadLine();
textBox2.AppendText($"[{datetime}] Received: {data}\r\n");
}
else
{
textBox2.AppendText($"[{datetime}] Error: There is no data to read\r\n");
}
}
編輯說:這應該讓你開始,你還想查看這個鏈接從 backgroundWorker1 訪問你的 UI,問題是你的 UI 在一個線程上,而你的 BackgroundWorker 在另一個線程上,如果沒有一些贊美,線程不能很好地協同工作代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.