using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { const int NUMBER_OF_DAQ = 5; ThreadMain[] my_thread_main = new ThreadMain[NUMBER_OF_DAQ]; Thread[] my_thread = new Thread[NUMBER_OF_DAQ]; Label[] vol_label = new Label[NUMBER_OF_DAQ]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { vol_label[0] = label1; vol_label[1] = label2; vol_label[2] = label3; vol_label[3] = label4; vol_label[4] = label5; for (int i = 0; i < NUMBER_OF_DAQ; i++) vol_label[i].Text = ""; button1.Text = "开始采集"; libVK70xNMC_DAQ2.Server_TCPOpen(8234); richTextBox1.Text += "==================================================\n"; richTextBox1.Text += "Start to monitor the DAQ device ...\n"; richTextBox1.Text += "Waiting ......\n"; backgroundWorker1.RunWorkerAsync(); } void ThreadMainCallBack(object obj) { ThreadMain t = (ThreadMain)obj; if (t.msg != string.Empty) { try { if (this.InvokeRequired) { this.Invoke(new EventHandler(delegate { richTextBox1.Text += t.msg; })); } } catch { ; } } // if (t.size > 0) { try { if (this.InvokeRequired) { this.Invoke(new EventHandler(delegate { richTextBox1.Text += "DAQ [" + String.Format("{0:00}", t.index) + "] the number of data points is read: " + String.Format("{0:00}\n", t.size); vol_label[t.index].Text = String.Format("{0:f8}", t.buffer[0]) + " " + String.Format("{0:f8}", t.buffer[1]) + " " + String.Format("{0:f8}", t.buffer[2]) + " " + String.Format("{0:f8}", t.buffer[3]); })); } } catch { ; } } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int index = 0; int count = 0; while (true) { if (backgroundWorker1.CancellationPending) { e.Cancel = true; break; } libVK70xNMC_DAQ2.Server_Get_ConnectedClientNumbers(ref count); if ((index != count) && (index < NUMBER_OF_DAQ)) { my_thread_main[index] = new ThreadMain(); my_thread_main[index].callback = ThreadMainCallBack; my_thread[index] = new Thread(new ParameterizedThreadStart(my_thread_main[index].main)); my_thread_main[index].thread = my_thread[index]; my_thread[index].Start(index); index++; try { if (this.InvokeRequired) { this.Invoke(new EventHandler(delegate { richTextBox1.Text += "Found the connected DAQ device: " + count + "\n"; })); } } catch { ; } } Thread.Sleep(10); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (backgroundWorker1.IsBusy) { backgroundWorker1.CancelAsync(); } for (int i = 0; i < NUMBER_OF_DAQ; i++) { if (my_thread_main[i] != null) { my_thread_main[i].thread.Abort(); } } } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "开始采集") { button1.Text = "停止采集"; for (int i = 0; i < NUMBER_OF_DAQ; i++) { if (my_thread_main[i] != null) { my_thread_main[i].stop = false; my_thread_main[i].start = true; } } } else { button1.Text = "开始采集"; for (int i = 0; i < NUMBER_OF_DAQ; i++) { if (my_thread_main[i] != null) { my_thread_main[i].start = false; my_thread_main[i].stop = true; } } } } void LimitLine(int maxLength) { if (richTextBox1.Lines.Length > maxLength) { int moreLines = richTextBox1.Lines.Length - maxLength; string[] lines = richTextBox1.Lines; Array.Copy(lines, moreLines, lines, 0, maxLength); Array.Resize(ref lines, maxLength); richTextBox1.Lines = lines; } } private void richTextBox1_TextChanged(object sender, EventArgs e) { LimitLine(16); richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.SelectionLength = 0; richTextBox1.Focus(); } } }