VorTex.Zerg
VorTex.Zerg

Reputation: 73

Using Threads in the windowsForms

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();


    }

}

}

Upvotes: 2

Views: 70

Answers (1)

Tshsmith
Tshsmith

Reputation: 140

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?

Upvotes: 3

Related Questions