跨螺纹交叉形式。显示带有进度条的初始屏幕 [英] Cross-thread cross-form. Display a splash screen with a progress bar

查看:18
本文介绍了跨螺纹交叉形式。显示带有进度条的初始屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案:

所以我设法找到了另一个教程http://www.codeproject.com/KB/dotnet/Yet_Another_Splash_Screen.aspx,源代码对我来说似乎更有意义。以下是我现在使用的代码。Main()保持不变。

Splash.cs

`

公共分部类FRM_Splash:Form { 委托无效进度委托(int百分比); 委托void SplashShowCloseDelegate();

    /// <summary>
    /// To ensure splash screen is closed using the API and not by keyboard or any other things
    /// </summary>
    bool CloseSplashScreenFlag = false;

    /// <summary>
    /// Base constructor
    /// </summary>
    /// 
    public Frm_Splash()
    {
        InitializeComponent();
        progress_Splash.Show();
        this.ClientSize = this.BackgroundImage.Size;
    }

    public void ShowSplashScreen()
    {
        if (InvokeRequired)
        {
            // We're not in the UI thread, so we need to call BeginInvoke
            BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen));
            return;
        }
        this.Show();
        Application.Run(this);
    }

    /// <summary>
    /// Closes the SplashScreen
    /// </summary>
    public void CloseSplashScreen()
    {
        if (InvokeRequired)
        {
            // We're not in the UI thread, so we need to call BeginInvoke
            BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen));
            return;
        }
        CloseSplashScreenFlag = true;
        this.Close();
    }

    /// <summary>
    /// Update text in default green color of success message
    /// </summary>
    /// <param name="Text">Message</param>
    public void Progress(int percent)
    {
        if (InvokeRequired)
        {
            // We're not in the UI thread, so we need to call BeginInvoke
            BeginInvoke(new ProgressDelegate(Progress), new object[] { percent });
            return;
        }
        // Must be on the UI thread if we've got this far
        progress_Splash.Value = percent;
        // Fade in the splash screen - looks pro. :D
       if (percent < 10)
            this.Opacity = this.Opacity + .15;
    }


    /// <summary>
    /// Prevents the closing of form other than by calling the CloseSplashScreen function
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void SplashForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (CloseSplashScreenFlag == false)
            e.Cancel = true;
    }
 }`

Form1.cs

public partial class Frm_Main : Form
{
    Frm_Splash frm_Splash = new Frm_Splash();

    public Frm_Main()
    {
        this.Hide();
        Thread splashthread = new Thread(new ThreadStart(frm_Splash.ShowSplashScreen));
        splashthread.IsBackground = true;
        splashthread.Start();
        InitializeComponent();
        CenterToScreen();
    }

    private void Frm_Main_Load(object sender, EventArgs e)
    {
        if (PassedAll() == true)
            FillMovieLB();
        if (FillMovieProgress == 100)
        {
            //Throw in this sleep so the user can see the progress bar reach all the way to the end.
            Thread.Sleep(1000);
            this.Show();
            frm_Splash.CloseSplashScreen();
            this.Activate();
        }
    }

原始问题

G‘day all,

我对C#编程非常陌生,我对http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx教程和在我的应用程序中实现它有问题。我发现有点难以理解问题出在哪里。我知道让这个闪屏工作有很多事情,但我就是想不通。

当我启动程序时,会显示Frm_Main,您可以看到listbox正在填充,因为我把它放在BackgroundWorker.DoWork()中,然后工作完成后会显示我的frm_Splash。显然,它的工作方式是,在Frm_Main上的工作过程中会显示frm_Splash,进度条会显示加载的进度(这部分我还没有实现)。

编辑:我可能说不清楚,但问题是:如何在工作完成期间和主窗体显示之前显示闪屏?

谢谢大家。:)

以下是我的代码:

   static Frm_Splash frm_Splash = new Frm_Splash();

    public delegate void ShowFormDelegate();

    public void ShowForm()
    {
        frm_Splash.Show();
    }

    public Frm_Main()
    {
        InitializeComponent();
        CenterToScreen();
        if (PassedAll() == true)
        {
            back_loadprog.RunWorkerAsync();  
        }
    }

   private void back_loadprog_DoWork(object sender, DoWorkEventArgs e)
    {
        Invoke(new ShowFormDelegate(ShowForm));
        Invoke(new FillMovieLBDelegate(FillMovieLB));
    }

推荐答案

这里,有一些代码...适合我。

开机画面:

namespace Screens.Forms
{
    public partial class Splash : DevExpress.XtraEditors.XtraForm
    {
        public Splash()
        {
            InitializeComponent();
        }
        string RandomLoadingMessage()
        {
            string[] lines ={
                "Pripremam warp pogon",
                "Moj drugi ekran za učitavanje je brži, probaj njega",
                "Verzija programa koju imam u testiranju imala je smiješnije poruke"
            };
            return lines[new Random().Next(lines.Length)];
        }
        public void RandomizeText()
        {
            lblMessage.Text = RandomLoadingMessage();
        }
        private void Splash_Load(object sender, EventArgs e)
        {
            RandomizeText();
        }
        private static Splash _splash;
        private static bool _shouldClose;

        static void ThreadFunc()
        {
            _splash = new Splash();
            _splash.Show();
            while (!_shouldClose)
            {
                Application.DoEvents();
                Thread.Sleep(100);
                if (new Random().Next(1000) < 10)
                {
                    _splash.Invoke(new MethodInvoker(_splash.RandomizeText));
                }
            }
            for (int n = 0; n < 18; n++)
            {
                Application.DoEvents();
                Thread.Sleep(60);
            }
            if (_splash != null)
            {
                _splash.Close();
                _splash = null;
            }
        }
        static public void ShowSplash()
        {
            _shouldClose = false;
            Thread t = new Thread(ThreadFunc);
            t.Priority = ThreadPriority.Lowest;
            t.Start();
        }
        internal static void RemoveSplash()
        {
            _shouldClose = true;
        }
        internal static void ShowSplash(List<string> fromTwitterMessages)
        {
            ShowSplash();
        }
    }
}

显示为:

Splash.ShowSplash();

做您需要的工作,然后完成:

Splash.RemoveSplash();

这篇关于跨螺纹交叉形式。显示带有进度条的初始屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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