base.Show() throwing InvalidOperationException

485 views Asked by At

I am having an issue with base.Show() throwing an InvalidOperationException. There are a couple of issues with this:

  1. The error is shown in vs2012 only when I show "Common Language Runtime Exceptions" in the Debug -> Exceptions menu, but not if I run it without that.
  2. When running the program outside of vs2012, a messagebox shows showing the error with the stack trace shown at the bottom of this post.
  3. I have tried to research InvalidOperationException thrown on base.Show() online, but have been unsuccessful in finding anything relevant to base.Show() specifically.

What the program is doing is opening a form, and when a link is clicked, it opens a DocViewer window using the following code:

 private void paperVisionLink_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            AnalRunSeq sequence = (bob.Resources["AnalRunSeqsCollection"] as CollectionViewSource).View.CurrentItem as AnalRunSeq;

            if (sequence != null)
            {
                try
                {
                    var pveView = this.ShowCOCDocumentForWorkorder((sequence.Sample.WorkOrderID));
                }
                catch (Exception ex)
                {
                    ELI_Data.DataLogger.Logger.Error("Error starting papervision window", ex);
                }
            }
        }
        catch
        {
            MessageBox.Show("Cannot find COC document for that Work Order.");
        }
    }

    public Window ShowCOCDocumentForWorkorder(string workorder)
    {
        PaperVision pve = new PaperVision("bluser", "bluser");
        pve.SubmitSearchCriteria("WORK ORDER\tCATEGORY", workorder.ToUpper() + "\tCOC");
        XDocument response = pve.ExecuteQuery();

        XElement documentXml = response.Descendants("DOC").FirstOrDefault();

        PVEWindow pveView = null;

        if (!documentXml.IsNull())
        {
            pveView = new PVEWindow();
            pveView.Show(
                string.Format("{0}/HttpInterface.asp", EnergyDatabase.Setup.DocImaging.WebService),
                EnergyDatabase.Setup.DocImaging.EntityID.Value,
                pve.SessionID,
                EnergyDatabase.Setup.DocImaging.DocProjects.Single(dp => dp.Project == "LOGIN").ProjectID.Value,
                documentXml.Attribute("DOCID").Value);
        }

        return pveView;
    }

The pveView.Show method is where the base.Show() method is throwing the execption:

public void Show(string url, int entityId, string sessionId, int projId, string docId)
    {
        try
        {
            base.Show();  //exception thrown here
            this.PvdmDocView.InitComm(url, entityId, sessionId, projId, docId);
        }
        catch (Exception ex)
        {
            Logger.Error("Error opening papervision viewer", ex);
            throw;
        }
    }

Here's the exception that is thrown when the program is run outside of visual studio:

************** Exception Text **************
System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed.
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithoutLicense(Guid clsid)
at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
at System.Windows.Forms.AxHost.CreateInstance()
at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.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)

The strangest thing with this is that although this exception is thrown, if you attempt to continue after the message box is shown, everything runs correctly, so I'm not sure how to resolve this issue. Any help would be greatly appreciated!

EDIT

I have update the above post to remove the threading and add the ShowCOCDocumentForWorkorder method to the main window class. This should solve the threading issue that was occurring before. The only issue that now occurs that should be much easier to solve is to fix the base.Show() method throwing the InvalidOperationException error. The class that is used in does not allow the Invoke method to be used, although I am not sure why. Here is the full class code for the PVEWindow class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Microbiology
{
    using System.Windows.Forms;

    /// <summary>
    /// Interaction logic for PVEWindow.xaml
    /// </summary>
    public partial class PVEWindow : Window
    {
        public PVEWindow()
        {
            this.InitializeComponent();
            base.Show();
        }

        public void Show(string url, int entityId, string sessionId, int projId, string docId)
        {
            //base.Show();
            try
            {
                this.PvdmDocView.InitComm(url, entityId, sessionId, projId, docId);
            }
            catch (Exception ex)
            {
                ELI_Data.DataLogger.Logger.Error("Error opening papervision viewer", ex);
                throw;
            }
        }
    }
}
1

There are 1 answers

0
DiggityCS On

I was able to solve the issue by adding the following code to the Show method before the base.Show() call:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);