【发布时间】:2015-08-27 17:31:22
【问题描述】:
我遇到了 base.Show() 引发 InvalidOperationException 的问题。这有几个问题:
- 只有当我在 Debug -> Exceptions 菜单中显示“Common Language Runtime Exceptions”时才会在 vs2012 中显示该错误,但如果我在没有它的情况下运行它则不会。
- 在 vs2012 之外运行程序时,会出现一个消息框,显示错误,并在本文底部显示堆栈跟踪。
- 我曾尝试在线研究 base.Show() 上引发的 InvalidOperationException,但未能成功找到与 base.Show() 相关的任何内容。
程序正在做的是打开一个表单,当点击一个链接时,它会使用以下代码打开一个 DocViewer 窗口:
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;
}
pveView.Show 方法是 base.Show() 方法抛出执行的地方:
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;
}
}
这是程序在 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)
最奇怪的是,虽然抛出了这个异常,但如果你在显示消息框后尝试继续,一切运行正常,所以我不知道如何解决这个问题。任何帮助将不胜感激!
编辑
我已更新上述帖子以删除线程并将 ShowCOCDocumentForWorkorder 方法添加到主窗口类。这应该可以解决之前发生的线程问题。现在发生的唯一应该更容易解决的问题是修复引发 InvalidOperationException 错误的 base.Show() 方法。中使用的类不允许使用 Invoke 方法,尽管我不知道为什么。 以下是 PVEWindow 类的完整类代码:
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;
}
}
}
}
【问题讨论】:
-
在您致电
show()之前,您的窗口似乎已关闭。检查this question 的答案。 -
我已经查看了您所指的问题,但无法使用建议的答案。 InvokeRequired 不适用于我的课程,尽管我不确定为什么。此外,在调用 show 之前窗口不会关闭。 pveView.Closed 在使用 show() 方法的 ShowCOCDocumentForWorkorder 调用之后调用。
-
PVEWindow不是控件(例如表单)吗?如果是这样,Windows 可能会终止线程,因此在尝试显示之前关闭控件。 -
是的,PVEWindow 是一个 32 位 COM 对象。如果是这种情况,我希望窗口不会显示线程是否被杀死,但事实并非如此。抛出错误后,我继续,窗口显示并正常运行。
-
你试过用
this.Invoke((MethodInvoker)(() => base.Show()));替换base.Show();吗?我确定这与“调用线程无法访问此对象,因为不同的线程拥有它”有关。
标签: c# .net winforms show invalidoperationexception