Show和ShowDialog不起作用

本文关键字:不起作用 ShowDialog Show | 更新日期: 2023-09-27 18:10:07

这是我以前从未见过的。
当我调用(new System.Windows.Forms.Form()). showdialog()时,窗体会显示一毫秒左右,然后消失。
我追踪了这些电话,得到了这个:

System.Windows.Forms.Form.Dispose
System.ComponentModel.Component.Dispose
System.Windows.Forms.Application.ThreadWindows.Dispose
System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.Form.ShowDialog Esfand.Program.Main c#

我已经尝试了任何想到的解决这个问题。

尽管在展示这个表单之前我已经展示了一个登录表单。
我不认为在登录表单上有什么特别的东西(通常是无聊的东西,连接到服务器,发送凭据,接收数据)。我使用主线程的形式。我有消息传递和消息循环的经验。我在登录表单中使用了线程。

编辑:对Cody Gray建议的澄清:

这是我在void Main(string[])中的内容:

LoginForm login = new LoginForm ();
login.ShowDialog ();//works
if (login.DialogResult == DialogResult.OK)
{
    MainForm f = new MainForm ();
    f.ShowDialog ();//won't work
}

在新线程中创建并显示MainForm使一切重新开始工作。但是每种形式都会出现随机错误,这使得这个解决方案不够好。

编辑2:
FormClosing事件甚至不会触发。

System.Windows.Forms.Form A;
A = new Form();
A.FormClosing += new FormClosingEventHandler((sender, e) => { System.Diagnostics.Debugger.Break();/*won't work. tried Breakpoints and other stuff too*/ });
A.ShowDialog();

编辑3:HandleDestroyed事件堆栈跟踪:

>   Esfand.exe!Esfand.Program.Main.AnonymousMethod__1(object sender = {System.Windows.Forms.Form}, System.EventArgs e = {System.EventArgs}) Line 50 + 0x6 bytes C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.OnHandleDestroyed(System.EventArgs e) + 0x9e bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.OnHandleDestroyed(System.EventArgs e) + 0x13 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Control.WmDestroy(ref System.Windows.Forms.Message m) + 0x54 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x547 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) + 0x6d bytes 
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg = 2, System.IntPtr wparam, System.IntPtr lparam) + 0x15e bytes    
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DestroyHandle() + 0xf7 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Control.DestroyHandle() + 0x3e3 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Control.Dispose(bool disposing) + 0x347 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.Dispose(bool disposing) + 0x19 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Form.Dispose(bool disposing) + 0x26a bytes    
    System.dll!System.ComponentModel.Component.Dispose() + 0x1b bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadWindows.Dispose() + 0xb3 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows() + 0x12d bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x58e bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = 4, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.Application.ModalApplicationContext}) + 0x593 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x81 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) + 0x765 bytes    
    Esfand.exe!Esfand.Program.Main(string[] a = {string[0]}) Line 51 + 0x10 bytes   C#

Show和ShowDialog不起作用

这个东西使我的程序中的每个表单都抛出一个唯一的错误(例如:'无法注册拖放事件处理程序')

这是对代码核心问题的强烈暗示。当您拥有AllowDrop属性设置为true的控件时,将调用RegisterDragDrop()本地函数。它在创建窗体的本机窗口时被调用。不幸的是,如果你有64位版本的Windows,并且你强迫你的程序在32位模式下运行,那么这是一个非常糟糕的时间来引发任何异常。这个问题的答案

RegisterDragDrop()失败的原因很少。但一个。我们已经可以从你的代码片段中看出你一直在摆弄Program.cs。它的样板版本看起来像这样:

static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]                // <=== Here!!!
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

我在重要的代码行上画了一个大箭头。[STAThread]属性在任何GUI程序中都是必不可少的。它告诉CLR它应该初始化COM并将程序的主线程配置为单线程公寓。公寓是一个非常重要的COM实现细节,其范围超出了这个答案。如果缺少该属性,则程序的主线程将加入MTA,即多线程公寓。对于非线程安全的代码,如拖放、剪贴板和shell对话框,这是一个充满敌意的地方。

忘记使用该属性会导致令人困惑的异常。尤其是当你的开发机器启动64位版本的Vista或Win7时,Windows版本在关键时刻会出现异常,如链接答案中所解释的那样。

您的Program.Main()方法的正确版本,该版本不存在此问题,并且使用了推荐的实践:

    [STAThread]                // <=== Don't forget this
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using (var login = new LoginForm()) {
            if (login.ShowDialog() != DialogResult.OK) return;
        }
        Application.Run(new MainForm());
    }

尝试检查是否抛出线程异常错误。检查你是否在Application_ThreadException事件中看到任何东西。

static void Main()
{
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    try
    {
        //Your existing code
    }
    catch (Exception ex)
    {
    }
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
}
编辑:

另一个选择是显式地将MainForm设置为正在创建的newForm的所有者。

newForm.ShowDialog(MainForm);

我有一种感觉,所有者被设置为您的登录表单默认是关闭的,这是自动关闭您的新表单

它看起来像IMsoComponentManager.FPushMessageLoopP()将调用Application.ThreadContext.DisposeThreadWindows()当有一个WM_QUIT消息在事件队列

您是否在LoginForm的按钮事件处理程序中发布退出消息?

尝试将表单定义为类成员。而不是在函数中。

您的Main方法看起来很奇怪。我想你漏打了一个Application.Run()的电话。由于您不希望应用程序在登录表单关闭后立即退出,因此您可能需要一个ApplicationContext。MSDN有一个例子:http://msdn.microsoft.com/en-us/library/ms157901.aspx

另一种可能性是调用Application.Run()与一个不可见的形式作为参数,然后显示其他形式(并且永远不会关闭,直到应用程序应该退出),但这是一个相当丑陋的hack在我看来

我的理解是表单需要在应用程序上下文中运行。

我继承了一些从Main上下文中启动几个表单的代码,方式如下:

var form1 = Form1();
Application.Run(form1);
// form returns, check public method form.ButtonPushed etc
if (form.Button1Pushed)
{
    var form2 = Form2();
    Application.Run(form2);
}

这将成功启动多个表单。

这看起来并不是一种很优雅的方式,但是它是有效的…