I have problem with WPViewPDF library - it randomly (but quite often) throws an exception when I try to dispose pdf viewer control.
Exception details:
Code
************** Exception Text **************System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at WPViewPDF.PDFViewer.DefWndProc(Message& m) at System.Windows.Forms.Control.WmSetFocus(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)
As it is exception thrown from unmanaged code I cannot caught it in my .NET app and application crashes.
Code
private void PDFForm_FormClosing(object sender, FormClosingEventArgs e) { try { if (pdfViewer != null && !pdfViewer.IsDisposed && pdfViewer.IsHandleCreated) { pdfViewer.Clear(); pdfViewer.Dispose(); // <- Here this exception is thrown pdfViewer = null; GC.Collect(); } } catch (Exception ex) { Log.TraceError("Cannot dispose pdfViewer: {0}", ex, ex.Message); } }
What can be the reason this exception is thrown?
I use the latest release of the library together with .NET 4.0 framework.
EDIT: I've found that this exception is thrown almost always when I open about 40MB encrypted PDF, scroll to 2nd or 5th page and immediately close PDF Viewer form.
Temporary solution that seems to solve this problem on my clients PCs is to hide PDF viewer form and wait ~10 sec. before disposing controls.
Code
private void PDFForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (pdfViewer != null && !pdfViewer.IsDisposed && pdfViewer.IsHandleCreated)
{
Hide();
Thread.Sleep(10000);
pdfViewer.Clear();
pdfViewer.Dispose(); // <- Here this exception is thrown
pdfViewer = null;
GC.Collect();
}
}
catch (Exception ex)
{
Log.TraceError("Cannot dispose pdfViewer: {0}", ex, ex.Message);
}
}
Alles anzeigen