public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; tipStatus.Text = "就绪"; tipStatus2.Text = "请选择本地库文件"; TcpHelper = new TCPHelper(); LogHelper = new LogHelper(this.GetType()); }
/// <summary> /// 多线程发送报警消息 /// </summary> private void SendTestDataMultiThread() { //将所有待发送的报警消息分成N组,N由MaxThreadCount决定 int count = 0; IList<String>[] listArray = GetMessageListArray(out count); tProcessBar.Maximum = count; tProcessBar.Value = 0; int groupIndex = 0; foreach (IList<String> list in listArray) { if (list != null && list.Count > 0) { try { TCPHelper tcpHeler = null; if(groupIndex < TcpHelpers.Count) { tcpHeler = TcpHelpers[groupIndex]; } else { tcpHeler = new TCPHelper(); TcpHelpers.Add(tcpHeler); } SendTestData(list, tcpHeler); } catch (Exception ex) { ShowMessageText(ex.Message); } groupIndex++; } } }
private void SendTestData(IList<String> list, TCPHelper tcpHeler) { if(!tcpHeler.Connected) tcpHeler.Connect(AlarmIP, AlarmPort); Task task = new TaskFactory().StartNew(() => { foreach (String msg in list) { try { byte[] retVal = tcpHeler.SendData(msg); lock (lockProcessBarVal) { tProcessBar.Value += 1; if(tProcessBar.Value ==1) loopDone.Set(); } ShowMessageText(msg + ":" + System.Text.Encoding.ASCII.GetString(retVal)); Thread.Sleep(SendTimeSpan); } catch (Exception ex) { ShowMessageText(ex.Message); } } }); }