private void StopConnect() { ACSUpdater.StopUpdate(); QPLCUpdater.StopUpdate(); LSUpdater.StopUpdate(); ToggleListen(toggleType.CLOSE); connected = false; toggleConnectButton.Text = "Connect!"; UpdateUIThread.Abort(); EnableMenu(); }
private void StartConnect(out bool result) { result = true; IODataCollection.queryList.Clear(); if (!ACSUpdater.CreateQuery() || !QPLCUpdater.CreateQuery() || !LSUpdater.CreateQuery() || IODataCollection.controllerNameList.Count == 0) { MessageBox.Show("无控制器或有的控制器无IO", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); result = false; return; } DisableMenu(); CancellationTokenSource ts = new CancellationTokenSource(); CancellationToken tk = ts.Token; Task[] tasks = new Task[3] { Task.Run(() => ACSUpdater.Connect(tk)), Task.Run(() => QPLCUpdater.Connect(tk)), Task.Run(() => LSUpdater.Connect(tk)), }; bool connected = false, hasErr = false; try { Task.WaitAll(tasks); connected = true; } catch (AggregateException ae) { hasErr = true; ts.Cancel(); try { Task.WaitAll(tasks); } catch { } Invoke(new Action(() => MessageBox.Show(ae.InnerException.Message.ToString()))); } while (!connected && !hasErr) { Thread.Sleep(100); } if (connected) { try { ACSUpdater.StartUpdate(); QPLCUpdater.StartUpdate(); LSUpdater.StartUpdate(); CheckUpdateErrorThread(); connected = !connected; toggleConnectButton.Text = "Disconnect"; ThreadUpdateUI(); if (LimitWizardThread.IsAlive) { LimitWizardThread.Abort(); } this.connected = true; } catch (Exception e) { hasErr = true; MessageBox.Show(e.Message); } } if (hasErr) { result = false; StopConnect(); } }