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) {
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);
source share