Procházet zdrojové kódy

用Timer替换原来的线程循环!

张泳健 před 8 roky
rodič
revize
9b524903bd

+ 30 - 7
GWSocketClient/Form1.Designer.cs Zobrazit soubor

@@ -72,6 +72,8 @@
72 72
             this.cmslistboxRightMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
73 73
             this.rightMenuCopyCmd = new System.Windows.Forms.ToolStripMenuItem();
74 74
             this.rightMenuClearInfo = new System.Windows.Forms.ToolStripMenuItem();
75
+            this.timerHeartBeat = new System.Windows.Forms.Timer(this.components);
76
+            this.StripMenuItemLoSucHb = new System.Windows.Forms.ToolStripMenuItem();
75 77
             this.groupBox1.SuspendLayout();
76 78
             this.menuStrip1.SuspendLayout();
77 79
             this.statusStrip1.SuspendLayout();
@@ -346,6 +348,7 @@
346 348
             // 心跳HToolStripMenuItem
347 349
             // 
348 350
             this.心跳HToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
351
+            this.StripMenuItemLoSucHb,
349 352
             this.ToolStripMenuItemHbStart,
350 353
             this.toolStripSeparator1,
351 354
             this.toolStripSeparator3,
@@ -359,24 +362,28 @@
359 362
             // 
360 363
             // ToolStripMenuItemHbStart
361 364
             // 
365
+            this.ToolStripMenuItemHbStart.CheckOnClick = true;
366
+            this.ToolStripMenuItemHbStart.Enabled = false;
362 367
             this.ToolStripMenuItemHbStart.Name = "ToolStripMenuItemHbStart";
363
-            this.ToolStripMenuItemHbStart.Size = new System.Drawing.Size(172, 22);
364
-            this.ToolStripMenuItemHbStart.Text = "登录成功就开启";
368
+            this.ToolStripMenuItemHbStart.Size = new System.Drawing.Size(196, 22);
369
+            this.ToolStripMenuItemHbStart.Text = "开启心跳";
370
+            this.ToolStripMenuItemHbStart.CheckStateChanged += new System.EventHandler(this.ToolStripMenuItemHbStart_CheckStateChanged);
371
+            this.ToolStripMenuItemHbStart.Click += new System.EventHandler(this.ToolStripMenuItemHbStart_Click);
365 372
             // 
366 373
             // toolStripSeparator1
367 374
             // 
368 375
             this.toolStripSeparator1.Name = "toolStripSeparator1";
369
-            this.toolStripSeparator1.Size = new System.Drawing.Size(169, 6);
376
+            this.toolStripSeparator1.Size = new System.Drawing.Size(193, 6);
370 377
             // 
371 378
             // toolStripSeparator3
372 379
             // 
373 380
             this.toolStripSeparator3.Name = "toolStripSeparator3";
374
-            this.toolStripSeparator3.Size = new System.Drawing.Size(169, 6);
381
+            this.toolStripSeparator3.Size = new System.Drawing.Size(193, 6);
375 382
             // 
376 383
             // ToolStripMenuItemHb30abc
377 384
             // 
378 385
             this.ToolStripMenuItemHb30abc.Name = "ToolStripMenuItemHb30abc";
379
-            this.ToolStripMenuItemHb30abc.Size = new System.Drawing.Size(172, 22);
386
+            this.ToolStripMenuItemHb30abc.Size = new System.Drawing.Size(196, 22);
380 387
             this.ToolStripMenuItemHb30abc.Text = "下面输入心跳时间";
381 388
             // 
382 389
             // toolStripTextBoxHbtime
@@ -389,12 +396,12 @@
389 396
             // toolStripSeparator2
390 397
             // 
391 398
             this.toolStripSeparator2.Name = "toolStripSeparator2";
392
-            this.toolStripSeparator2.Size = new System.Drawing.Size(169, 6);
399
+            this.toolStripSeparator2.Size = new System.Drawing.Size(193, 6);
393 400
             // 
394 401
             // ToolStripMenuItemLogHb
395 402
             // 
396 403
             this.ToolStripMenuItemLogHb.Name = "ToolStripMenuItemLogHb";
397
-            this.ToolStripMenuItemLogHb.Size = new System.Drawing.Size(172, 22);
404
+            this.ToolStripMenuItemLogHb.Size = new System.Drawing.Size(196, 22);
398 405
             this.ToolStripMenuItemLogHb.Text = "是否打印心跳";
399 406
             this.ToolStripMenuItemLogHb.Click += new System.EventHandler(this.ToolStripMenuItemLogHb_Click);
400 407
             // 
@@ -492,6 +499,20 @@
492 499
             this.rightMenuClearInfo.Text = "清空信息(&L)";
493 500
             this.rightMenuClearInfo.Click += new System.EventHandler(this.rightMenuClearInfo_Click);
494 501
             // 
502
+            // timerHeartBeat
503
+            // 
504
+            this.timerHeartBeat.Interval = 1000;
505
+            this.timerHeartBeat.Tick += new System.EventHandler(this.timerHeartBeat_Tick);
506
+            // 
507
+            // StripMenuItemLoSucHb
508
+            // 
509
+            this.StripMenuItemLoSucHb.Checked = true;
510
+            this.StripMenuItemLoSucHb.CheckOnClick = true;
511
+            this.StripMenuItemLoSucHb.CheckState = System.Windows.Forms.CheckState.Checked;
512
+            this.StripMenuItemLoSucHb.Name = "StripMenuItemLoSucHb";
513
+            this.StripMenuItemLoSucHb.Size = new System.Drawing.Size(196, 22);
514
+            this.StripMenuItemLoSucHb.Text = "登录成功同时开启心跳";
515
+            // 
495 516
             // Form1
496 517
             // 
497 518
             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
@@ -566,6 +587,8 @@
566 587
         private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
567 588
         private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
568 589
         private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItemLogHb;
590
+        private System.Windows.Forms.Timer timerHeartBeat;
591
+        private System.Windows.Forms.ToolStripMenuItem StripMenuItemLoSucHb;
569 592
     }
570 593
 }
571 594
 

+ 40 - 32
GWSocketClient/Form1.cs Zobrazit soubor

@@ -56,52 +56,36 @@ namespace GWSocketClient
56 56
             port = tbPort.Text.Trim();
57 57
 
58 58
             uiDelegate = addToListboxItem;
59
+
60
+            timerHeartBeat.Enabled = ToolStripMenuItemHbStart.Checked;
59 61
         }
60 62
 
61 63
 
62 64
         #region 处理心跳相关的
63 65
 
64
-        //开启心跳连接
65
-        private void startHeartBeatThread()
66
+        private void timerHeartBeat_Tick(object sender, EventArgs e)
66 67
         {
67 68
             Thread thread = new Thread(heartBeatThread);
68 69
             thread.Start(this);
69 70
         }
71
+/*
72
+        //开启心跳连接
73
+        private void startHeartBeatThread()
74
+        {
75
+        }*/
70 76
 
71 77
         private void heartBeatThread(object o)
72 78
         {
73
-            while (hbRunFlag)
74
-            {
75
-                int interval = 5;
76
-                if (!toolStripTextBoxHbtime.Text.Trim().Equals(""))
77
-                {
78
-                    interval = int.Parse(toolStripTextBoxHbtime.Text.Trim());
79
-                }
80
-                Thread.Sleep(interval * 1000);
81
-
82
-
83
-                if (hbRunFlag)
84
-                {
85
-                    string hbCmd = vcarecityProtocolBean.ProtocolStartSymbol;
86
-                    hbCmd += tbEquipmentAddress.Text.Trim() + "840000";
87
-                    hbCmd = Utils.calcCrcCode(hbCmd);
88
-                    byte[] ts = HexUtil.hexToBytes(hbCmd);
79
+            string hbCmd = vcarecityProtocolBean.ProtocolStartSymbol;
80
+            hbCmd += tbEquipmentAddress.Text.Trim() + "840000";
81
+            hbCmd = Utils.calcCrcCode(hbCmd);
82
+            byte[] ts = HexUtil.hexToBytes(hbCmd);
89 83
 
90
-                    sockeTcpClients.SendData(ts);
84
+            sockeTcpClients.SendData(ts);
91 85
 
92
-                    if (ToolStripMenuItemLogHb.Checked)
93
-                    {
94
-                        this.Invoke(this.uiDelegate, new object[] {string.Format("发送到服务端-心跳:[{0}]", hbCmd)});
95
-                    }
96
-                }
97
-            }
98
-            try
99
-            {
100
-                this.Invoke(this.uiDelegate, new object[] { "心跳线程结束!" });
101
-            }
102
-            catch (Exception e)
86
+            if (ToolStripMenuItemLogHb.Checked)
103 87
             {
104
-                Console.WriteLine(e);
88
+                this.Invoke(this.uiDelegate, new object[] {string.Format("发送到服务端-心跳:[{0}]", hbCmd)});
105 89
             }
106 90
         }
107 91
 
@@ -140,8 +124,12 @@ namespace GWSocketClient
140 124
                             addToListboxItem(string.Format("客户端信息{0}", sks.ex));
141 125
                             checkLogin(true);
142 126
                             sendLoginCmd();
127
+                            if (StripMenuItemLoSucHb.Checked)
128
+                            {
129
+                                ToolStripMenuItemHbStart.Checked = true;
130
+                            }
143 131
                             hbRunFlag = true;
144
-                            startHeartBeatThread();
132
+//                            startHeartBeatThread();
145 133
                             break;
146 134
                         case SocketHelper.Sockets.ErrorCodes.TrySendData:
147 135
                             addToListboxItem("状态信息:ErrorCodes");
@@ -534,6 +522,7 @@ namespace GWSocketClient
534 522
 
535 523
         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
536 524
         {
525
+            timerHeartBeat.Enabled = false;
537 526
             try
538 527
             {
539 528
                 sockeTcpClients.Stop();
@@ -551,6 +540,14 @@ namespace GWSocketClient
551 540
 
552 541
         private void btnTextCmd_Click(object sender, EventArgs e)
553 542
         {
543
+            //beta test tool
544
+            if (tfTmpSendData.Text.Trim().ToLower().Equals("pre"))
545
+            {
546
+                btnPressureTest.Visible = !btnPressureTest.Visible;
547
+                tbPresureTestCount.Visible = !tbPresureTestCount.Visible;
548
+                label4.Visible = !label4.Visible;
549
+            }
550
+
554 551
 //            string code = Utils.calcCrcCode("7e7e7e123456789abc20000701010107030001");
555 552
 //            code = Utils.calcCrcCode("7e7e7e123456789abc10000501010107023f");
556 553
 //            Console.WriteLine(code);
@@ -592,13 +589,24 @@ namespace GWSocketClient
592 589
                 btnLogin.Enabled = false;
593 590
                 btnQuickLogout.Enabled = true;
594 591
                 btnSendData.Enabled = true;
592
+                ToolStripMenuItemHbStart.Enabled = true;
595 593
             }
596 594
             else
597 595
             {
598 596
                 btnLogin.Enabled = true;
599 597
                 btnQuickLogout.Enabled = false;
600 598
                 btnSendData.Enabled = false;
599
+                ToolStripMenuItemHbStart.Enabled = false;
601 600
             }
602 601
         }
602
+
603
+        private void ToolStripMenuItemHbStart_CheckStateChanged(object sender, EventArgs e)
604
+        {
605
+            timerHeartBeat.Enabled = ToolStripMenuItemHbStart.Checked;
606
+        }
607
+
608
+        private void ToolStripMenuItemHbStart_Click(object sender, EventArgs e)
609
+        {
610
+        }
603 611
     }
604 612
 }

+ 3 - 0
GWSocketClient/Form1.resx Zobrazit soubor

@@ -126,6 +126,9 @@
126 126
   <metadata name="cmslistboxRightMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127 127
     <value>258, 17</value>
128 128
   </metadata>
129
+  <metadata name="timerHeartBeat.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
130
+    <value>438, 17</value>
131
+  </metadata>
129 132
   <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
130 133
   <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
131 134
     <value>

binární
GWSocketClient/bin/Release/dbfile/ID_INFO.accdb Zobrazit soubor