Show splash screen while loading main form

I am trying to make the popup screen first, and after the spike, MainForm will appear. But the progress bar, which I have in the splash screen, does not fall at the end of the panel. And the program continues to work and does not work.

How can I show a splash screen while loading the main form?

My code This is something like this:

 public partial class SplashForm : Form { public SplashForm() { InitializeComponent(); } private void SplashForm_Load(object sender, EventArgs e) { timer1.Enabled = true; timer1.Start(); timer1.Interval = 1000; progressBar1.Maximum = 10; timer1.Tick += new EventHandler(timer1_Tick); } public void timer1_Tick(object sender, EventArgs e) { if (progressBar1.Value != 10) { progressBar1.Value++; } else { timer1.Stop(); Application.Exit(); } } } 

Here is the first part of MainForm code:

 public partial class MainForm : Form { public MainForm() { InitializeComponent(); Application.Run(new SplashForm()); } } 
0
source share
1 answer

There are different ways to create a screensaver. It’s better to separate the logic for displaying and closing the splash screen from the logic of your main form.

To do this, you can create a LoadCompleted event, and then subscribe to it in the Program class and show and close your splash screen.

Here is the implementation of what I described above:

1- In MainForm add the MainForm event, and then override the OnLoad method to raise the event. (Perhaps the Shown event Shown applicable instead of our custom event.)

 public event EventHandler LoadCompleted; protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.OnLoadCompleted(EventArgs.Empty); } protected virtual void OnLoadCompleted(EventArgs e) { var handler = LoadCompleted; if (handler != null) handler(this, e); } private void MainForm_Load(object sender, EventArgs e) { //Just for test, you can make a delay to simulate a time-consuming task //In a real application here you load your data and other settings } 

2- In the Program class, show SplashForm , then subscribe to the LoadCompleted event of your MainForm and show MainForm , then in LoadCompleted , close SplashForm .

 static class Program { static SplashForm mySplashForm; static MainForm myMainForm; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Show Splash Form mySplashForm = new SplashForm(); if (mySplashForm != null) { Thread splashThread = new Thread(new ThreadStart( () => { Application.Run(mySplashForm); })); splashThread.SetApartmentState(ApartmentState.STA); splashThread.Start(); } //Create and Show Main Form myMainForm = new MainForm(); myMainForm.LoadCompleted += MainForm_LoadCompleted; Application.Run(myMainForm); if(!(mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed)) mySplashForm.Invoke(new Action(() => { mySplashForm.TopMost = true; mySplashForm.Activate(); mySplashForm.TopMost = false; })); } private static void MainForm_LoadCompleted(object sender, EventArgs e) { if (mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed) return; mySplashForm.Invoke(new Action(() => { mySplashForm.Close(); })); mySplashForm.Dispose(); mySplashForm = null; myMainForm.TopMost = true; myMainForm.Activate(); myMainForm.TopMost = false; } } 
+3
source

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


All Articles