AutoRelease.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using AGV_WPF.Models;
  12. using AGV_WPF_Global;
  13. namespace AGV_WPF
  14. {
  15. public partial class AutoRelease : Form
  16. {
  17. bool auto = false;
  18. private delegate void InvokeDelegate();
  19. int route = 80;
  20. string[] text = null;
  21. public AutoRelease()
  22. {
  23. InitializeComponent();
  24. }
  25. private void AutoRelease_Load(object sender, EventArgs e)
  26. {
  27. this.FormClosed += new FormClosedEventHandler(AutoRelease_FormClosed);
  28. }
  29. void AutoRelease_FormClosed(object sender, FormClosedEventArgs e)
  30. {
  31. //throw new NotImplementedException();
  32. auto = false;
  33. }
  34. private void button1_Click(object sender, EventArgs e)
  35. {
  36. Button btn = (Button)sender;
  37. if (btn.Text == "启动")
  38. {
  39. textBox2.Invoke(new InvokeDelegate(delegate
  40. {
  41. if (route.ToString() != textBox2.Text)
  42. {
  43. route = int.Parse(textBox2.Text);
  44. }
  45. }));
  46. textBox1.Invoke(new InvokeDelegate(delegate
  47. {
  48. text = textBox1.Text.Split(',');
  49. }));
  50. btn.Text = "停止";
  51. auto = true;
  52. Thread t = new Thread(new ThreadStart(autoRun));
  53. t.Priority = ThreadPriority.Lowest;
  54. t.Start();
  55. }
  56. else
  57. {
  58. btn.Text = "启动";
  59. auto = false;
  60. }
  61. }
  62. public void autoRun()
  63. {
  64. bool bb = false;
  65. ParallelLoopResult result2;
  66. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  67. Thread t2=null;
  68. try
  69. {
  70. if (text != null && text.Length > 0&&auto)
  71. {
  72. List<string> list = text.ToList();
  73. result2 = Parallel.ForEach<string>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, key =>
  74. {
  75. List<AGVInfoModel> results = listModel.FindAll(o => o.MARKNUM.ToString() == key&&
  76. (o.RUNSTATUS_Cur == 0x41 )&& o.ConnectStatus==true);
  77. if (results != null && results.Count > 0)
  78. {
  79. if ((DateTime.Now - results[0].LastModifyMarkTime).TotalSeconds >= 3)
  80. {
  81. AGV_WPF.Tools.DispatcherTool.ReleaseAgvOnStop(int.Parse(key), route, 0, 1);
  82. }
  83. }
  84. });
  85. if (result2.IsCompleted)
  86. {
  87. Thread.Sleep(2000);
  88. t2 = new Thread(new ThreadStart(autoRun));
  89. t2.Priority = ThreadPriority.Lowest;
  90. t2.Start();
  91. }
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. bb = true;
  97. }
  98. finally
  99. {
  100. if (bb)
  101. {
  102. if (t2 != null) { t2.Abort(); }
  103. Thread.Sleep(2000);
  104. t2 = new Thread(new ThreadStart(autoRun));
  105. t2.Priority = ThreadPriority.Lowest;
  106. t2.Start();
  107. }
  108. }
  109. }
  110. void autoRun2()
  111. {
  112. while (true)
  113. {
  114. if (auto)
  115. {
  116. textBox2.Invoke(new InvokeDelegate(delegate
  117. {
  118. if (route.ToString() != textBox2.Text)
  119. {
  120. route = int.Parse(textBox2.Text);
  121. }
  122. }));
  123. textBox1.Invoke(new InvokeDelegate(delegate
  124. {
  125. string[] text = textBox1.Text.Split(',');
  126. if (text.Length > 0)
  127. {
  128. for (int i = 0; i < text.Length; i++)
  129. {
  130. AGV_WPF.Tools.DispatcherTool.ReleaseAgvOnStop(int.Parse(text[i]), route, 0, 1);
  131. }
  132. }
  133. }));
  134. }
  135. Thread.Sleep(3000);
  136. }
  137. }
  138. }
  139. }