如何修复出现在闪屏后面的消息框? [英] How to fix message boxes appearing behind splash screen?

查看:37
本文介绍了如何修复出现在闪屏后面的消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WinForms 应用程序的主窗口加载缓慢(最多 20 秒,取决于参数),因此它需要一个启动画面.

My WinForms app's main window is slow to load (up to 20 seconds, depending on arguments), so it needs a splash screen.

主窗口构造函数很慢,因为它运行了数千行代码(其中一些超出了我的影响).有时此代码会弹出消息框.

The main window constructor is slow because it exercises thousands of lines of code (some of it beyond my influence). Sometimes this code pops up message boxes.

我尝试了两种启动画面设计,它们各有问题.有什么更好的想法吗?

I've tried two splash screen designs, they each have problems. Any better ideas?

static void Main(string[] args)
{
    var splash = !args.Contains("--no-splash");
    if (splash)
    {
        var bw = new BackgroundWorker();
        bw.DoWork += (sender, eventArgs) => ShowSplash();
        bw.RunWorkerAsync();
    }

    var app = new FormMain(args); // slow. sometimes opens blocking message boxes.
    Application.Run(app);
}

private static void ShowSplash()
{
    using (var splash = new FormSplash())
    {
        splash.Show();
        splash.Refresh();
        Thread.Sleep(TimeSpan.FromSeconds(2));
    }
}

问题:

  1. 启动画面有时会在主窗口打开之前过期(用户认为应用已崩溃)
  2. 主窗口有时会在飞溅关闭时最小化.

带有 WindowsFormsApplicationBase 的启动画面

sealed class App : WindowsFormsApplicationBase
{
    protected override void OnCreateSplashScreen()
    {
        this.SplashScreen = new FormSplash();
    }

    protected override void OnCreateMainForm()
    {
        // slow. sometimes opens blocking message boxes.
        this.MainForm = new FormMain(this.CommandLineArgs);
    }
}

问题:

  1. 任何打开的 MessageBox 都会无声地出现在启动屏幕后面.用户不会注意到它并认为应用卡住了.
  2. 如果初始屏幕是始终在顶部",则消息框将无法访问且无法点击.

推荐答案

最后,将慢代码从构造函数移到 OnShown 事件.

In the end, moved the slow code from the constructor to a handler for the OnShown event.

按照 Hans Passant 的建议使用 WindowsFormsApplicationBase 作为启动画面,仔细检查剩余的构造函数代码以确保它永远不会打开消息框.

Used WindowsFormsApplicationBase for splash screen as Hans Passant suggested, carefully checked the remaining constructor code to make sure it'll never open an message boxes.

这篇关于如何修复出现在闪屏后面的消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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