2

I Designed and coded 1 program which has a Splash Screen for loading.I used the thread in the main form which will Close the Loading form completely and Display the main form. this actions takes about 5000 Ms. But when i use the thread in the program, after 5000 MS, Splash Screen will disappear and the main form will display Correctly. But after 1 or 2 seconds after main form displayed , the program Close and it goes to the BreakMode ! Donnow why. Here is my code. Maybe my code has problem. Thanks in advance for your suggestions.

public MainForm()
{
    Thread t = new Thread(new ThreadStart(StartForm));
    t.Start();
    Thread.Sleep(7000);
    InitializeComponent();
    t.Abort();
}
public void StartForm()
{
    Application.Run(new frmSplashScreen());
}

(Using Visual Studio 2015 Enterprise) __________________________UPDATE___________________________ I used this method https://stackoverflow.com/a/393870/17034 and changed the forms name into mine. But again when i use the program i face with BreakingMode !! This made me crazy What is your suggestion guys ? Is there any way to disable this mode ?! here is my new Codes :

using System;

using System.Windows.Forms;

using Microsoft.VisualBasic.ApplicationServices;

namespace University

{

static class Program

{

    [STAThread]

    static void Main(string[] args)

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        new MyApp().Run(args);

    }

}

class MyApp : WindowsFormsApplicationBase

{

    protected override void OnCreateSplashScreen()

    {

        this.SplashScreen = new frmSplash();

    }

    protected override void OnCreateMainForm()

    {

        System.Threading.Thread.Sleep(4500);

        this.MainForm = new MainForm();


    }

}

}

Community
  • 1
  • 1
  • 1
    Use the [built-in support for splash screens](http://stackoverflow.com/a/393870/17034) instead of trying to spin your own. – Hans Passant Feb 11 '17 at 16:20
  • @HansPassant There is no way to Correct this code instead of Change ? – VorTex.Zerg Feb 11 '17 at 16:26
  • The original code does not make much sense... Usually a single thread is used for UI and you **never** sleep in UI thread. **You should also avoid to abort a thread.** In a properly designed application, you don't use such hacks. – Phil1970 Feb 11 '17 at 17:55

1 Answers1

3

Your program will have a main.cs that starts your from.cs and subsequently shows the form. What you want to do is edit the main.cs so that this other form is shown in an a non asynchronous manner. Then use a timer on your splash screen that starts on launch and then closes after the duration you want it open. Then the main.cs will continue on as normal.

However, this answer is based on work I've done and may not be relevant to your project. Could you post a git link or the rest of your code please and I'll updated my answer?

Tshsmith
  • 142
  • 1
  • 8
  • 1
    Thank for your help and attentions. I tried http://stackoverflow.com/a/393870/17034 this way and it helped me. – VorTex.Zerg Feb 11 '17 at 16:37