C#Winforms在单个线程上启动第二个消息循环是无效的操作.使用Form.ShowDialog代替 [英] C# Winforms Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead

查看:1170
本文介绍了C#Winforms在单个线程上启动第二个消息循环是无效的操作.使用Form.ShowDialog代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:我的应用程序以许可证验证表开始,并且客户的许可证是否有效.它应该启动主表单.

Summary: My application starts off with a license validation form and if the client's license is valid. It should launch the main form.

但是,我收到以下错误:在单个线程上启动第二个消息循环是无效的操作.改用Form.ShowDialog.

However I am receiving the following error: Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.

我的实现:

static class Program
{
    static AppStartUp appStartUp_;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        appStartUp_ = new AppStartUp();
        appStartUp_.OnValidationSuccessful += OnValidationSuccessful;

        Application.Run(appStartUp_);
    }

    static void OnValidationSuccessful()
    {
        appStartUp_.OnValidationSuccessful -= OnValidationSuccessful;
        appStartUp_.Close();
        appStartUp_.Dispose();

        //Application.ExitThread();
        Application.Run(new Manager());
    }
}

我在做错什么吗?

推荐答案

在该OnValidationSuccessful事件期间,您的第一个Application.Run仍在运行.假设该事件正在关闭表单,请尝试设置变量:

Your first Application.Run is still running during that OnValidationSuccessful event. Assuming that event is closing the form, try setting a variable instead:

static bool appOK = false;

然后在您的事件中,将其设置为true:

then in your event, set it to true:

static void OnValidationSuccessful()
{
  appOK = true;
}

然后在您的Main过程中,它看起来像这样:

then in your Main procedure, it would look like this:

Application.Run(appStartUp_);
if (appOK) {
  Application.Run(new Manager());
}

这篇关于C#Winforms在单个线程上启动第二个消息循环是无效的操作.使用Form.ShowDialog代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆