简体   繁体   中英

Exiting c# app from splash screen

I am using a splash screen for ac# which runs on startup and checks the app license.

I show the splash from the main form like this:

public partial class Form1 : Form
{
    static bool stopThreads = false;
    static bool gridChanged = false;

    public Form1()
    {
        InitializeComponent();
        Thread th = new Thread(new ThreadStart(DoSplash));
        th.Start();
        th.Join();
    }

    private void DoSplash()
    {
        Splash sp = new Splash();
        sp.ShowDialog();
    }

Now from the splash form I am trying exit the application when the license is invalid, but it only exits the splash and it enters the main form.

I tried exiting with :

Enviroment.Exit();

Application.Exit();

Form1 f = new Form1();
this.Close();

But none closes the main form, only the splash.

How can I close the entire app from the splash form class?

Yes, these calls only cause the thread to exit. You created a new thread. There's little point to be gracious about it in this case, Environment.Exit(1) will get the job done. The huff-and-puff version is Control.BeginInvoke() to run code on the main UI thread. You'll need a reference to Form1 to make that call.

Btw, you'll also have a big problem with SystemEvents, they run on the wrong thread because the very first window you created was created on thread other than the main UI thread. The most typical mishap is a deadlock when you lock and unlock the work station. You'll need to wait until at least one window is created on the UI thread. Form1's OnLoad() method override or Load event would be a good place to start the splash. Or just use the built-in support for splash screens.

You could use Application.Exit() or Environment.Exit() .

These probably aren't the "cleanest" way to shut down your app, but if you're just bailing at the splash screen, it's unlikely it'll cause any issues.

Edit: If you want to quit without showing the splash screen at all if the licence is invalid, you should check the licence before showing the splash screen, and just exit before then.

Never introduce multithreading in application unless absolutely necessary.
As Sir Walter put it,

Else thou shalt enter into a world of pain.

Moreover, any UI interactions, such as displaying a window or working with controls, must be done on main thread only.

If you want to do something while the form is on the screen, call Show instead of modal ShowDialog so execution does not get blocked.

Application.Exit ()

will do nicely if you call it on the main thread, as you should.

If you want to show splash screen before main form is shown, you should not do it in main form 's InitializeComponent . Instead, change code in Program.cs to show splash screen first:

Application.Run (new SplashScreenForm ());

Somewhere in SplashScreenForm (I don't know why you need it at all, honestly) you should check for license, and if it's fine, close the window, create MainForm instance and call its ShowDialog . If it's bad—just close the window, and since it was the last form, application would stop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM