// 关闭设备 private void BnClose_Click(object sender, EventArgs e) { // 关闭海康相机 if ((chooseHIK) && (!chooseBasler)) { try { // 取流标志位清零 if (hikCanGrab == true) { hikCanGrab = false; hikGrabThread.Join(); } if (m_BufForDriver != IntPtr.Zero) { Marshal.Release(m_BufForDriver); } // 释放相机 hIKVisionCamera.DestroyCamera(); // 控件操作 SetCtrlWhenClose(); } catch { MessageBox.Show("关闭相机失败,请重启!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // 关闭basler相机 else if ((chooseBasler) && (!chooseHIK)) { try { if (baslerCanGrab == true) { baslerCanGrab = false; baslerGrabThread.Join(); } // 释放相机 baslerCamera.DestroyCamera(); // 控件操作 SetCtrlWhenClose(); } catch { MessageBox.Show("关闭相机失败,请重启!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } }
// 关闭设备 private void BnClose_Click(object sender, EventArgs e) { // 关闭海康相机 if ((chooseHIK) && (!chooseBasler)) { try { // 取流标志位清零 if (hikCanGrab == true) { hikCanGrab = false; hikGrabThread.Join(); } if (m_BufForDriver != IntPtr.Zero) { Marshal.Release(m_BufForDriver); } // 释放相机 hIKVisionCamera.DestroyCamera(); // 控件操作 SetCtrlWhenClose(); } catch { MessageBox.Show("关闭相机失败,请重启!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // 关闭basler相机 else if ((chooseBasler) && (!chooseHIK)) { try { if (baslerCanGrab == true) { baslerCanGrab = false; baslerGrabThread.Join(); } // 释放相机 baslerCamera.DestroyCamera(); // 控件操作 SetCtrlWhenClose(); } catch { MessageBox.Show("关闭相机失败,请重启!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (chooseUSB && (!chooseBasler) && (!chooseHIK)) { //关闭USB相机 if (usbCanGrab) { this.usblabelinfo.Text = "正在关闭USB相机"; this.usblabelinfo.Visible = true; Func <bool> delFunc = () => { return(usbDeviceSource.close()); }; IAsyncResult result = delFunc.BeginInvoke(null, null); Thread loading = new Thread(() => { int count = 0; while (!result.IsCompleted) //(!result.AsyncWaitHandle.WaitOne(500)) { // 询问是否完成操作,否则做其它事情。 if (count < 4) { this.Invoke(new Action(() => { usblabelinfo.Text += "."; this.Cursor = Cursors.WaitCursor; })); } else { count = 0; this.Invoke(new Action(() => { usblabelinfo.Text = "正在关闭USB相机"; })); } count++; } //获取异步操作结果 if (delFunc.EndInvoke(result)) { // wait ~ 3 seconds count = 0; for (int i = 0; i < 6; i++) { System.Threading.Thread.Sleep(500); if (count < 4) { this.Invoke(new Action(() => { this.usblabelinfo.Text += "."; })); } else { count = 0; this.Invoke(new Action(() => { this.usblabelinfo.Text = "正在启动USB相机."; })); } count++; i++; } // 控件操作 SetCtrlWhenClose(); // 清空pictureBox this.Invoke(new Action(() => { this.Cursor = Cursors.Default; this.pictureBox1.Image = null; })); usbCanGrab = false; } else { MessageBox.Show("关闭相机失败,请重启!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }); loading.IsBackground = true; loading.Start(); } } }