Windows 窗体应用程序 - 启动画面标签未更新 [英] Windows Form Application - Splash screen label not updating

查看:30
本文介绍了Windows 窗体应用程序 - 启动画面标签未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 windows 窗体应用程序,它应该显示一个带有标签字段的初始屏幕,我想在主窗体(称为welcome.cs)在后台加载时更新该标签字段.启动画面显示 &隐藏得很好,但标签不会更新.

I have a windows form application which is supposed to show a splash screen with a label field that I want to update as the main form (called welcome.cs) loads in the background. The splash screen shows & hides just fine, but the label doesn't update.

我做了很多研究,但还没有完全找到解决方案.

I've done a lot of research but haven't quite found the solution.

程序.cs

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

            SplashScreen splashscreen = new SplashScreen();
            splashscreen.ShowSplashScreen();

            Welcome welcome = new Welcome(splashscreen); //Takes some time to load
            splashscreen.CloseForm();
            Application.Run(welcome);

        }
    }

Splashscreen.cs

Splashscreen.cs

public partial class SplashScreen : Form
    {
        //Delegate for cross thread call to close
        private delegate void CloseDelegate();
        private delegate void UpdateStatusDelegate(string status);
        private static SplashScreen splashScreen;
        private Thread thread = null;

        public SplashScreen()
        {
            InitializeComponent();
        }

        public void ShowSplashScreen()
        {
            // Make sure it is only launched once.

            if (splashScreen != null)
                return;
            thread = new Thread(ShowForm);
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        static private void ShowForm()
        {
            splashScreen = new SplashScreen();
            Application.Run(splashScreen);
        }

        public void CloseForm()
        {
            splashScreen.Invoke(new CloseDelegate(CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            splashScreen.Close();
        }

        public void UpdateStatus(string status)
        {
            splashScreen.Invoke(new UpdateStatusDelegate(UpdateStatusInternal), status);
        }

         private void UpdateStatusInternal (string status)
        {
             if (splashScreen != null && splashScreen.IsHandleCreated)
             {
                 lblStatus.Text = status;
             }

        }
    }

欢迎.cs

public Welcome(Splashscreen splashscreen)
{
        InitializeComponent();
        //Code to log the user into the system

        splashScreen.UpdateStatus("Logging in...");
        //my expectation is that UpdateStatus call will update the label displayed on the splash screen but it doesn't.

        //Do more stuff.....
}

它是否与多线程有关,还是因为我在调用 UpdateStatus 之前在welcome.cs 中创建了一个新的splashscreen 实例?我该如何解决这个问题?

Does it have something to do with multi-threading or is it because im creating a new instance of splashscreen in welcome.cs before calling UpdateStatus? How would I get around this?

推荐答案

您可以执行以下操作

 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);

     string[] args = Environment.GetCommandLineArgs();

     // Creates the Splash
     splash = new FrmSplash();

     //Opens the Splash in a new Thread, this way any gifs, progress bars, lablels changes will work because the main thread isnt blocked
     var t = Task.Factory.StartNew(() =>
     {
         splash.ShowDialog();
     });

      while (!splash.Created) // wait the splash screen form load process
          System.Threading.Thread.Sleep(300);

     UpdateSplashMessage("Loading the program... Please wait");         
     // Some slow initialization code.
     // ...

     //Close splash screen
     CloseSplash();

     Application.Run(args);
 }

 static void CloseSplash()
 {
     splash.Invoke(new MethodInvoker(() =>
     {
         splash.Close(); // Closes the splash that is running in the other thread
     }));
 }

 static void UpdateSplashMessage(string msg)
 {
     splash.Invoke(new MethodInvoker(() =>
     {
         splash.AtualizarMensagem(msg);
     }));
 }

请注意,您需要在初始屏幕表单中创建一个名为 AtualizarMensagem(string str) 的方法,如下所示

Note that you will need to create a method called AtualizarMensagem(string str) in your splash screen form, like this

public void AtualizarMensagem(string novaMsg)
{
    lblCarregando.Text = novaMsg;
}

我的有用的snipets"文件夹中有这个代码,它总是对我有用.希望这会有所帮助.

I have this code in my "useful snnipets" folder, it always works for me. Hope this helps.

这篇关于Windows 窗体应用程序 - 启动画面标签未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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