SEHException when disposing WPViewPDF

  • 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.

    • Offizieller Beitrag

    Hi,

    I do not know what Windows does when you dispose a control. WPViewPDF needs some time to close its painting thread, it does so when the window is destroyed. It should not be possible to through it out of the memory without some delay.

    I recommend, if you have multiple instances, have one instance which is loaded at start and not destroyed until program ends. This prevents the DLL to be unloaded which would cause a problem with the window subclass.


    Julian

  • Hi,

    Thanks for reply. I've already read that keeping one "alive" control in the whole application life-time cycle could solve many problems that's why I have (hidden) one in my main form.

    Is it possible to get notified when WPViewPDF control closed its painting thread?

    My temporary solution to wait arbitrarily chosen period of time works, but I would not like it in my final application.

    10 seconds seems to be enough for WPViewPDF control to close all it's threads in any system I've tested - but this value cannot be sure (especially in some older systems / slower PCs) - and the worst thing is that disposing the control throws uncatchable exception that makes whole application crash?