我在捕捉和处理异常方面遇到了一些麻烦。 我的(简化)应用程序有一个图片框,显示来自连接的网络摄像头的实时视频。我使用Metrilius Metricam SDK(http://www.metrilus.de/blog/portfolio-items/metricam/)连接到网络摄像头。 使用backgroundworker,我调用camera.update函数,抓取位图并将图片框设置为它。只要用户点击Enter,就会抓取并存储当前帧。 如前所述,当使用来自摄像头的最新bmp更新图片框时,会在完全随机的时间点抛出异常。正如您在下面的代码中看到的,我已将赋值包装在try / catch块中,但它并没有捕获它。我能做些什么来优雅地处理这件事?异常在图片框上绘制一个大的红色X,程序必须关闭并重新启动。 picturebox的sizemode属性设置为Zoom。
以下代码:
Private Sub BG1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BG1.DoWork
While Not BG1.CancellationPending
If pauseCamUpdate = False Then 'PausecamUpdate is a flag that is set to False during calls to store the current frame
i += 1
If i Mod 2 = 0 Then Continue While 'Dropping every nth frame to make the update smoother
If i > 10000 Then
i = 0
End If
Try
activeCam.Update()
pictPhoto.Image = CType(CType(activeCam, WebCam).CalcBitmap(), Bitmap)
'pictPhoto.Invalidate()
Application.DoEvents()
Catch exp As Exception
End Try
End If
End While
End Sub
异常文本减去加载的程序集:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
**System.InvalidOperationException: Object is currently in use elsewhere.**
at System.Drawing.Image.get_Height()
at System.Drawing.Image.get_Size()
at
System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr
wparam, IntPtr lparam)
感谢您的帮助。