为什么我的表格如此害羞?

Ref*_*din 1 .net c# multithreading splash-screen winforms

编辑2

好的,根据以下答案的建议,我删除了我的线程方法,现在我的程序看起来像这样: program.cs

 static void Main(){

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

FrmWWCShell FrmWWCShell = null;
var splash = new FrmSplash();
splash.SplashFormInitialized += delegate
                                {
                                    FrmWWCShell = new FrmWWCShell();
                                    splash.Close();
                                };
Application.Run(splash);
Application.Run(FrmWWCShell);
Run Code Online (Sandbox Code Playgroud)

}

FrmSplash.cs是这样的:

public partial class FrmSplash : Form
{

    public FrmSplash()
    {
        InitializeComponent();
    }

    protected override void  OnLoad(EventArgs e)
    {
        splashTimer.Interval = 1;
        splashTimer.Tick +=
            delegate { if (SplashFormInitialized != null) SplashFormInitialized(this, EventArgs.Empty); };
        splashTimer.Enabled = true;
    }

    public event EventHandler SplashFormInitialized;
}
Run Code Online (Sandbox Code Playgroud)

问题是它现在根本不起作用.启动屏幕弹出一瞬间,品牌进度条甚至没有初始化,然后消失,而我等待10秒钟的dll和主窗体出现,同时盯着什么......

让我现在严重困惑!


原始帖子 - >供参考

我实现了一个App Loading启动屏幕,该屏幕在一个单独的线程上运行,而所有的dll都在加载并且表单正在"绘制".这按预期工作.奇怪的是,现在当Splash表单退出时,它将我的主表单发送到后面,如果还有其他任何打开(即Outlook).我在Program.cs中启动线程,

static class Program
{
    public static Thread splashThread;

    [STAThread]
    static void Main()
    {
        splashThread = new Thread(doSplash);
        splashThread.Start();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmWWCShell());
    }

    private static void doSplash()
    {
        var splashForm = new FrmSplash();
        splashForm.ShowDialog();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,一旦我的FrmSearch_Shown事件被触发,我就结束它.

private void FrmSearch_Shown(object sender, EventArgs e)
    {
        Program.splashThread.Abort();
        this.Show();
        this.BringToFront();
    }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我试图在FrmSearch上调用Show()和/或BringToFront(),它仍然" "到后面.

我错过了什么?
我还能尝试什么?
我这样做是如此可怕地无知,这是我的过程造成的吗?
我应该提前退休吗?

感谢您的任何见解!


编辑1

我尝试将主表单上的TopMost属性设置为TRUE.这样可以防止我的表单隐藏,但它也会阻止用户查看任何其他应用程序.似乎对我有点自恋......

Cly*_*yde 5

首先,在主应用程序线程上完成UI工作非常重要.通过在后台线程上显示启动画面,我真的很惊讶你没有得到更严重的错误.

这是我用过的一种技术:

在您的启动表单上使用Application.Run而不是您的"真实"表单.

在您的启动表单中,有一个初始化事件:

public event EventHandler SplashFormInitialized
Run Code Online (Sandbox Code Playgroud)

创建一个在一毫秒内触发的计时器,并触发该事件.

然后在您的应用程序运行方法中,您可以加载您的真实表单,然后关闭您的启动表单并在真实表单上执行Application.Run

var realForm = null;

var splash = new SplashForm();
splash.SplashFormInitialized += delegate {
  // As long as you use a system.windows.forms.Timer in the splash form, this
  // handler will be called on the UI thread
  realForm = new FrmWWCShell();
  //do any other init
  splash.Close();
}
Application.Run(splash); //will block until the splash form is closed

Application.Run(realForm);
Run Code Online (Sandbox Code Playgroud)

飞溅可能包括:

overrides OnLoad(...)
{
   /* Using a timer will let the splash screen load and display itself before
      calling this handler
   */
   timer.Interval = 1;
   timer.Tick += delegate {
     if (SplashFormInitialized != null) SplashFormInitialized(this, EventArgs.Empty);
   };
   timer.Enabled = true; 
}
Run Code Online (Sandbox Code Playgroud)