ProgressBar while MainFrom is initialized

I have a windows forms application that needs to load a bunch of things before loading the main window. I thought this justified ProgressBar, so I thought that I was showing another form containing ProgressBar Controlusing the constructor of my main form.

Everything works fine, but if I try to put text in the Labelinput form, its contents will not be shown until the main form is loaded. Is there a way to avoid this, except by loading the input window first?

+3
source share
4 answers

You can show your SplashForm either from the main program or from the MainForm constructor, which doesn’t really matter. You see that until the download process is complete, no messages will be processed, and therefore, screen updates will not. ProgressBar is an exception, it starts its own thread for this reason.

A short solution is to make it SplashForm.Update()after changing the label. A little more important would be to create a separate thread with MessagePump (Application.Run). Here's a SO question and a few more potential customers.

+1
source

Warning: this post contains elements of self-promotion, o)

, , . ( SO Q & A) , , .

, ShowDialog, , . (, , ). , - , .

( , ):

using System;
using System.Windows.Forms;
public interface ISplashForm
{
    IAsyncResult BeginInvoke(Delegate method);
    DialogResult ShowDialog();
    void Close();
    void SetStatusText(string text);
}

using System.Windows.Forms;
public partial class SplashForm : Form, ISplashForm
{
    public SplashForm()
    {
        InitializeComponent();
    }
    public void SetStatusText(string text)
    {
        _statusText.Text = text;
    }
}

using System;
using System.Windows.Forms;
using System.Threading;
public static class SplashUtility<T> where T : ISplashForm
{
    private static T _splash = default(T);
    public static void Show()
    {
        ThreadPool.QueueUserWorkItem((WaitCallback)delegate
        {
            _splash = Activator.CreateInstance<T>();
            _splash.ShowDialog();
        });
    }

    public static void Close()
    {
        if (_splash != null)
        {
            _splash.BeginInvoke((MethodInvoker)delegate { _splash.Close(); });
        }
    }

    public static void SetStatusText(string text)
    {
        if (_splash != null)
        {
            _splash.BeginInvoke((MethodInvoker)delegate { _splash.SetStatusText(text); });
        }
    }
}

:

SplashUtility<SplashForm>.Show();
SplashUtility<SplashForm>.SetStatusText("Working really hard...");
SplashUtility<SplashForm>.Close();
+5

. BackgroundWorker.

Figo Fei :

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Maximum = 100;
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // This would be the load process, where you put your Load methods into.
        // You would report progress as something loads.

        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(100);
            backgroundWorker1.ReportProgress(i); //run in back thread
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method
    {
        progressBar1.Value = e.ProgressPercentage;
    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method
    {
        progressBar1.Value = progressBar1.Maximum;
    }

, .

+2

, , , , , . , :

Application.Run(new Form1());

Application.Run , , Form1 , ? , , .

I think the best approach is to first load the splash screen and start the workflow (you can use BackgroundWorker for this), which is time consuming. A progress bar will be displayed in the form of a splash screen, and you will periodically update it. Once the work is completed, you can close the splash screen and download the main form.

0
source

Source: https://habr.com/ru/post/1715881/


All Articles