打开winform的多个.NET控制台应用程序实例会导致间歇性访问冲突

时间:2017-03-21 01:38:11

标签: vb.net winforms console-application access-violation

我们有一个启动winform的.NET控制台应用程序(winform位于.NET dll中并合并到控制台exe中)。它用作打印路由器,并从网页启动。

主要方法如下:

Sub Main()
    Try
        Dim pf As PrintForm = New PrintForm()
        pf.ShowDialog()
    Catch ex As Exception
        MsgBox("An error has occured. Please contact your System Administrator.")
    End Try
End Sub

我们发现控制台应用程序的多个实例是打开的(某些客户端在Citrix环境中运行,其中多个用户登录到同一个操作系统,即使在正常环境中,用户也有多个浏览器并不罕见选项卡打开并从多个选项卡打印,然后启动exe的多个实例,我们间歇性地遇到以下错误:

  

异常消息:已显示的表单无法显示为   模态对话框。之前将表单的visible属性设置为false   调用showDialog。

我不明白为什么会发生这种情况,因为控制台exe的每个实例(由使用Runtime.exec的java applet启动)都应该在它自己的实例中启动。

为了解决上述问题,我尝试将代码更改为以下内容:

<STAThread>
Sub Main()
    Try
        Dim pf As PrintForm = New PrintForm()
        Application.Run(pf)
    Catch ex As Exception
        MsgBox("An error has occured. Please contact your System Administrator.")
    End Try
End Sub

我们不会间歇性地得到上述错误,而是我们现在间歇性地得到以下错误:

  

异常消息:在单个线程上启动第二个消息循环   不是有效的操作。改为使用Form.ShowDialog。

所以我似乎无法获胜。

有没有人有类似的问题,如果有的话你是如何解决的? 或者,任何人都可以提供一些指导/建议如何解决这个问题?

这是遗留代码,因此我没有将其设计为使用控制台应用程序,这是已经存在的解决方案。 Windows窗体是我可以尝试的,但我想看看是否有一个解决方案不涉及重写应用程序。还有一些事情要比我上面给出的简单例子更多,以传达我们正在做的事情的概念。

感谢您的时间。

更新#1:

好的,我创建了一个VB.NET WinForms应用程序,并将表单从控制台exe复制到WinForms应用程序中。

它仍然有同样的问题,除了我现在得到更少的调试信息。我可以使用运行的应用程序的单个实例来复制问题,但是现在当调试器中断时,(我可以在手动运行exe时复制它,并且在使用VS调试器运行时),我没有可用的反汇编和源代码行。 错误消息是:

  

附加信息:尝试读取或写入受保护的内存。   这通常表明其他内存已损坏。

调用堆栈是:

Application: PrintForm.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.NativeWindow.DefWndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.ToolTip.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.ToolTip+ToolTipNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(System.String[])
   at PrintForm.My.MyApplication.Main(System.String[])

当我收到此错误时,我是来自PDF查看器的alt + tab焦点,其程序窗口与应用程序窗口重叠,返回到WinForms应用程序。 如果PDF查看器窗口与WinForms应用程序窗口不重叠,则不会发生这种情况。

值得一提的是,启动PDF查看器(使用shell)的WinForms应用程序中的图标有一个工具提示。

再次是间歇性的。但它经常发生问题。

更新#2:

我现在已经复制了它,甚至没有启动PDF,只是从Windows资源管理器中将alt + tabing返回到Winforms应用程序并再次返回...其中Windows资源管理器窗口与Winforms应用程序重叠。所以Winforms Application在这种情况下所做的就是启动并变得可见。

0 个答案:

没有答案