123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.Threading.Tasks;
- using AGV_WPF.Models;
- using AGV_WPF_Global;
- namespace AGV_WPF
- {
- public partial class AutoRelease : Form
- {
- bool auto = false;
- private delegate void InvokeDelegate();
- int route = 80;
- string[] text = null;
-
- public AutoRelease()
- {
- InitializeComponent();
- }
- private void AutoRelease_Load(object sender, EventArgs e)
- {
- this.FormClosed += new FormClosedEventHandler(AutoRelease_FormClosed);
-
- }
- void AutoRelease_FormClosed(object sender, FormClosedEventArgs e)
- {
- //throw new NotImplementedException();
- auto = false;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- if (btn.Text == "启动")
- {
-
- textBox2.Invoke(new InvokeDelegate(delegate
- {
- if (route.ToString() != textBox2.Text)
- {
- route = int.Parse(textBox2.Text);
- }
- }));
- textBox1.Invoke(new InvokeDelegate(delegate
- {
- text = textBox1.Text.Split(',');
- }));
- btn.Text = "停止";
- auto = true;
- Thread t = new Thread(new ThreadStart(autoRun));
- t.Priority = ThreadPriority.Lowest;
- t.Start();
- }
- else
- {
- btn.Text = "启动";
- auto = false;
- }
- }
- public void autoRun()
- {
- bool bb = false;
- ParallelLoopResult result2;
- List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
- Thread t2=null;
- try
- {
- if (text != null && text.Length > 0&&auto)
- {
- List<string> list = text.ToList();
- result2 = Parallel.ForEach<string>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, key =>
- {
- List<AGVInfoModel> results = listModel.FindAll(o => o.MARKNUM.ToString() == key&&
- (o.RUNSTATUS_Cur == 0x41 )&& o.ConnectStatus==true);
- if (results != null && results.Count > 0)
- {
- if ((DateTime.Now - results[0].LastModifyMarkTime).TotalSeconds >= 3)
- {
- AGV_WPF.Tools.DispatcherTool.ReleaseAgvOnStop(int.Parse(key), route, 0, 1);
- }
- }
- });
- if (result2.IsCompleted)
- {
- Thread.Sleep(2000);
- t2 = new Thread(new ThreadStart(autoRun));
- t2.Priority = ThreadPriority.Lowest;
- t2.Start();
- }
- }
- }
- catch (Exception ex)
- {
- bb = true;
- }
- finally
- {
- if (bb)
- {
- if (t2 != null) { t2.Abort(); }
- Thread.Sleep(2000);
- t2 = new Thread(new ThreadStart(autoRun));
- t2.Priority = ThreadPriority.Lowest;
- t2.Start();
- }
- }
- }
- void autoRun2()
- {
- while (true)
- {
- if (auto)
- {
- textBox2.Invoke(new InvokeDelegate(delegate
- {
- if (route.ToString() != textBox2.Text)
- {
- route = int.Parse(textBox2.Text);
- }
- }));
- textBox1.Invoke(new InvokeDelegate(delegate
- {
- string[] text = textBox1.Text.Split(',');
- if (text.Length > 0)
- {
- for (int i = 0; i < text.Length; i++)
- {
- AGV_WPF.Tools.DispatcherTool.ReleaseAgvOnStop(int.Parse(text[i]), route, 0, 1);
- }
- }
- }));
- }
- Thread.Sleep(3000);
- }
- }
- }
- }
|