private void MainFrame_FormClosing(object sender, FormClosingEventArgs e) { if (isAdminstrator) { DialogResult result = MessageBox.Show("即将退出软件,是否保存当前设置?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); //新增取消软件关闭功能 if (result == DialogResult.Cancel) { e.Cancel = true; return; } else if (result == DialogResult.Yes) { try { AppManger.ProjectData.Instance.SaveProject(UserSetting.Instance.ProjectPath); } catch (Exception ex) { //Util.WriteLog(this.GetType(), ex); string message = ex.Message; MessageBox.Show("工程数据保存失败" + message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { DialogResult result = MessageBox.Show("确定要退出软件吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.Cancel) { e.Cancel = true; return; } } autoUnit.save_data_result(); //Application.Exit(); CameraManger.Close(); CommHandle.Instance.Close(); // PLC.PanasonicPLCComm.Instance.CloseCom(); //关闭所有相机处理线程 foreach (var item in autoUnit.runTheadDataDic.Values) { item.Stop(); } autoUnit.RunCommWriteDataThread.Stop(); autoUnit.RunCommReadDataThread.Stop(); Thread.Sleep(30); //关闭所有线程-此处有点暴力-待删除 Process.GetCurrentProcess().Kill(); Application.ExitThread(); }
/// <summary> /// 切换运行状态 /// </summary> public void btnRun_Click(object sender, EventArgs e) { int cameraCount = IniStatus.Instance.CamearCount; Dictionary <int, CameraBase> cameraDic = CameraManger.CameraDic; try { if (CommHandle.Instance.IsLink == false) { /*StatusManger status = StatusManger.Instance; * if (status.RuningStatus == RuningStatus.系统异常) * { * MessageBox.Show("系统出现异常,请检查设置", "提示", * MessageBoxButtons.OK, MessageBoxIcon.Error); * return; * }*/ if (CameraManger.CameraDic.Count > 0) { CameraManger.Close(); } if (CameraManger.Open() == false) { MessageBox.Show("没有相机连接,无法启动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } for (int i = 0; i < cameraCount; i++) { if (cameraDic.ContainsKey(i + 1) == false) { //Util.Notify(string.Format("请注意相机{0}未连接", i+1)); MessageHelper.ShowError(string.Format("相机{0}没有找到", i + 1)); } } foreach (int key in cameraDic.Keys) { if (key > cameraCount || key < 1) { cameraDic.Remove(key); Util.Notify(Level.Err, $"相机{key}名称ID超出相机个数设定范围,请用相机软件重新设定"); return; } } Util.Notify("相机打开完成"); foreach (var item in ToolsFactory.ToolsDic.Values) { Tools.CreateImage.CreateImageTool createImageTool = item[0] as Tools.CreateImage.CreateImageTool; createImageTool.OffLineMode = false; } LoadProject(false); CommHandle.Instance.CommunicationParam = UserSetting.Instance.MainDeviceComParam; //两对象同是CommunicationParam类型。 CommHandle.Instance.Open(); } else { if (MessageBox.Show("确定中止运行吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { CommHandle.Instance.IsLink = false; foreach (var item in CameraManger.CameraDic.Values) { item.ContinuousShotStop(); if (item.IsContinuousShot) { item.ContinuousShotStop(); } } } } } catch (Exception) { } }