using Go; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Forms; using TaskControl.gadget; using Module = TaskControl.gadget.Module; namespace TaskControl { public partial class MainForm : Form { //work_service work = new work_service(); //shared_strand strand; control_strand _mainStrand; generator _timeAction; Dictionary< int , Module> DicModules = new Dictionary< int , Module>(); int maxWorkerIndex = 2; bool continuous = false ; bool stop = false ; public MainForm() { InitializeComponent(); } private void MainForm_Load( object sender, EventArgs e) { //strand = new work_strand(work); //generator.go(strand, WorkerFlow); _mainStrand = new control_strand( this ); _timeAction = generator.make(_mainStrand, WorkerFlow); //generator.tgo(_mainStrand, WorkerFlow); } private void btnClear_Click( object sender, EventArgs e) { ClearMessage(); } private void btnControl_Click( object sender, EventArgs e) { AddMessage(btnControl.Text); switch (btnControl.Text) { case "启动任务" : btnControl.Text = "暂停任务" ; AddMessage( "===== 开始生产 =====" ); // // 可配置执行顺序 DicModules.Clear(); DicModules.Add(1, module1); DicModules.Add(2, module2); stop = false ; //work.run(); _timeAction.run(); break ; case "暂停任务" : btnControl.Text = "恢复任务" ; //work.stop(); _timeAction.suspend(); break ; case "恢复任务" : btnControl.Text = "暂停任务" ; //work.reset(); _timeAction.resume(); break ; } } private void btnContinuous_Click( object sender, EventArgs e) { AddMessage(btnContinuous.Text); switch (btnContinuous.Text) { case "循环生产" : btnContinuous.Text = "取消循环" ; continuous = true ; break ; case "取消循环" : btnContinuous.Text = "循环生产" ; continuous = false ; break ; } } private void btnStop_Click( object sender, EventArgs e) { AddMessage(btnStop.Text); switch (btnStop.Text) { case "结束生产" : btnStop.Text = "继续生产" ; stop = true ; break ; case "继续生产" : btnStop.Text = "结束生产" ; stop = false ; break ; } } async Task WorkerFlow() { // 罐仓同时加料 generator.children children = new generator.children(); foreach ( var item in DicModules) { children.go(() => AddPot(item.Key, item.Value)); } await children.wait_all(); // await Worker(); } async Task Worker( int index = 1) { if (index > maxWorkerIndex) { if (continuous) { index = 1; // 循环生产 ClearMessage(); } else { AddMessage( "===== 生产结束 =====" ); return ; } } AddMessage($ "顺序生产:{index}" ); var module = DicModules[index]; if ( null == module) return ; // 加料(串行) await AddPot(index, module); // 卸料(串行) await RemovePot(index, module); generator.children children = new generator.children(); children.go(() => MoveBelt(index)); // 皮带传输(并行) children.go(() => AddPot(index, module)); // 罐仓加料(并行) if (!stop) children.go(() => Worker(++index)); // 继续生产 await children.wait_all(); } /// <summary> /// 罐仓加料 /// </summary> async Task AddPot( int index, Module module) { var currentPotVal = module.Pot.Value; if (currentPotVal >= module.Pot.Maximum) { AddMessage($ "罐仓已满,跳过加料:【{index}】" ); return ; } AddMessage($ "开始加料:【{index}】" ); for ( int i = currentPotVal; i <= module.Pot.Maximum; i++) { module.Pot.Value = i; await generator.sleep(50); } AddMessage($ "结束加料:【{index}】" ); } /// <summary> /// 罐仓卸料 /// </summary> async Task RemovePot( int index, Module module) { AddMessage($ "开始卸料:【{index}】" ); for ( int i = module.Pot.Maximum; i > 0; i--) { module.Pot.Value = i; await generator.sleep(50); } AddMessage($ "结束卸料:【{index}】" ); } /// <summary> /// 皮带传输(工作几秒后停止-并行) /// </summary> /// <param name="index"></param> async Task MoveBelt( int index) { AddMessage($ "开始传输:【{index}】" ); var module = DicModules[index]; if ( null == module) return ; module.Belt.ConveyorDirection = ConveyorDirection.Forward; await generator.sleep(5000); module.Belt.ConveyorDirection = ConveyorDirection.None; AddMessage($ "结束传输:【{index}】" ); } public void AddMessage( string msg) { if (IsDisposed) return ; this .BeginInvoke((EventHandler)( delegate { if (rtbMsg.Lines.Length > 100) { rtbMsg.Clear(); } rtbMsg.AppendText(DateTime.Now.ToString( "yy-MM-dd HH:mm:ss" ) + " " + msg + "\r\n" ); Application.DoEvents(); })); } public void ClearMessage() { this .BeginInvoke((EventHandler)( delegate { rtbMsg.Clear(); })); } } } |