简体   繁体   English

闪屏线程

[英]Splash screen thread

So I have a splash screen which will have some time intensive code and I don't want it running in the main thread.所以我有一个闪屏,它会有一些时间密集的代码,我不希望它在主线程中运行。 I have made some code which should stop the thread and close the form, but it does not work.我已经编写了一些应该停止线程并关闭表单的代码,但它不起作用。 Any help is welcomed.欢迎任何帮助。

Code:代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Cobalt
{
    partial class Cobalt : Form
    {
        public static bool splashCont { get; set; }

        public Cobalt()
        {
            this.Text = "Cobalt V1.0.0";
            this.Width = 400;
            this.Height = 100;
            this.BackgroundImage = Properties.Resources.cobaltlgo;
            this.FormBorderStyle = FormBorderStyle.None;
            this.TopMost = true;
            this.StartPosition = FormStartPosition.CenterScreen;

            Thread splash = new Thread(new ThreadStart(splashLoadAction));
            splash.Start();

            if (splashCont)
            {
                splash.Abort();

                this.Close();
            }
        }

        private void splashLoadAction()
        {
            Thread.Sleep(5000);
            Cobalt.splashCont = true;
        }
    }
}

The program just stays at this screen: Screen EDIT: I was able to fix this by using the following code:程序只停留在这个屏幕上:屏幕编辑:我能够通过使用以下代码来解决这个问题:

Invoke((MethodInvoker)delegate { MyNextForm.Show(); });

Which invokes MyNextForm.Show() on the UI thread.它在 UI 线程上调用MyNextForm.Show()

If you want the work being done in the splash screen form, you can simplify this greatly.如果您希望以初始屏幕形式完成工作,则可以大大简化此操作。 This assumes that your five-second sleep is simulating the startup work being done.这假设您的五秒钟睡眠是模拟正在完成的启动工作。 This way the splash form will simply close itself out when the startup work is done.这样,当启动工作完成时,启动表单将简单地自行关闭。

partial class Cobalt : Form
{
    public Cobalt()
    {
        this.Text = "Cobalt V1.0.0";
        this.Width = 400;
        this.Height = 100;
        this.BackgroundImage = Properties.Resources.cobaltlgo;
        this.FormBorderStyle = FormBorderStyle.None;
        this.TopMost = true;
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Show();
        splashLoadAction();
        this.Close();
    }

    private void splashLoadAction()
    {
        Thread.Sleep(5000);
    }
}

You should put a timer in your splash screen form and close it once the time has been elapsed.您应该在启动屏幕表单中放置一个计时器,并在时间过去后关闭它。 You probably want to modify your application entry point so that this form is displayed before you start the main application form.您可能想要修改您的应用程序入口点,以便在您启动主应用程序表单之前显示此表单。

Well, in real life application, it might be more complicate than this if you want for example keep the splash displayed longer if the application is not ready or until the main form is actually displayed.好吧,在现实生活中的应用程序中,如果您希望例如在应用程序未准备好或直到实际显示主窗体之前使启动显示更长时间,则可能比这更复杂。

That might be useful if it take time to display the main application windows as having no windows displayed during a few second between the time the splash is closed and the application is visible might make your user think that the application has crashed...如果需要时间来显示主应用程序窗口,因为在启动关闭和应用程序可见之间的几秒钟内没有显示任何窗口,这可能会很有用,这可能会让您的用户认为应用程序已经崩溃......

Using timer, idle and visibility events, you can do anything you want exactly like you want once you understand how everything works.使用计时器、空闲和可见性事件,您可以在了解一切工作原理后完全按照自己的意愿做任何事情。

As you have thread.sleep in the thread, the main thread will continue executing so the code由于线程中有 thread.sleep,主线程将继续执行,因此代码

if (splashCont)
{
    splash.Abort();

    this.Close();
}

will execute well before you could set the splashCnt = true.将在您设置 splashCnt = true 之前执行良好。

Check if you really need to sleep the thread, if required then needs to think of workaround for it.检查您是否真的需要让线程休眠,如果需要,则需要考虑解决方法。

if you really want the thread to sleep than you could make the main thread wait for the sub thread to complete如果你真的想让线程休眠,那么你可以让主线程等待子线程完成

while (splash.IsAlive)
{
    Thread.Sleep(1000);
}

if (splashCont)
{
    splash.Abort();
    this.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM