Ma Dude
Ma Dude

Reputation: 557

Check for Application.Restart()

Is it possible to do something like the following:

bool wasRestarted = ???;
main() {
    if(wasRestarted) {
        MessageBox.Show("Welcome Back John");
    }
    Application.Restart();
}

This is specifically to the Application.Restart and NOT soft closing and reopening.

Only way I can think of right now is by creating a Setting Value:

Set it to 1 before restarting, then check for 1.

There has to be a better way then this?

Selected Answer worked perfect! As usual, thanks to the community for bringing a new feature to my brain :P

I used this to do a workaround for releasing a Mutex on Application.Restart on another thread. Since Mutex's are Thread Locked, I couldn't release a mutex on a login funtion before restarting, causing the Restart to return a locked mutex and couldnt continue. With this, I could know if it restarted, then do a simple while with a delay until the first "Application" actually closed and prematurely Program's Main() closed off resulting in the Mutex being cleared. The while() will then continue, and now my app works like normal!

Obviously for my needs, this wasn't the most ideal outcome, but it does work, and thats all I can ask for.

Upvotes: 1

Views: 1517

Answers (4)

Reza Aghaei
Reza Aghaei

Reputation: 125197

If it's about restarting the application, you don't need to store a setting.

If it's not about restarting and it's about the next time which user opens the application, then you definitely need to store a setting.

Here in this answer I'll share the solution to say "Welcome back!" to user, after restarting the application. To do so. Here in the following example, using the source code of Application.Restart I've implemented a Restart method for Program class and passed a command-line argument which shows application has been restarted:

To use, it's enough to call Program.Restart().

static class Program
{
    private static string restartArg =  "-restart";
    public static void Restart()
    {
        var startInfo = System.Diagnostics.Process.GetCurrentProcess().StartInfo;
        startInfo.FileName = Application.ExecutablePath;
        var exit = typeof(Application).GetMethod("ExitInternal",
                            System.Reflection.BindingFlags.NonPublic |
                            System.Reflection.BindingFlags.Static);
        exit.Invoke(null, null);
        startInfo.Arguments = restartArg;
        System.Diagnostics.Process.Start(startInfo);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var restarted = Environment.CommandLine.Contains(restartArg);
        if (restarted)
            MessageBox.Show("Welcome back!");
        Application.Run(new Form1());
    }
}

If you like to use the original commandline arguments and manipulate it rather than eliminating it, then take a look at this post:

Upvotes: 0

huysentruitw
huysentruitw

Reputation: 28091

You can use a temporary environment variable for that:

// Set environment variable before calling Restart
Environment.SetEnvironmentVariable("MYAPP_RESTART", "1");
Application.Restart();

// Detect restart:
var wasRestarted = Environment.GetEnvironmentVariable("MYAPP_RESTART");

if (wasRestarted == "1")
{
    // Your app was restarted
    Environment.SetEnvironmentVariable("MYAPP_RESTART", "0");
}

Upvotes: 3

DaveEP
DaveEP

Reputation: 1496

Do you have access to the file system? If so, write a config file that saves your exit state. If exiting normally save as (say) 0, when restarting save as a 1 and then read this file when starting up. When starting for the first time this file may not be present (unless it's part of your installer) so assume a 0 if not present.

If you are on Windows you could also use a registry setting too. It's not hard.

Upvotes: 3

Attersson
Attersson

Reputation: 4866

You could pass a (command line alike) argument to main when you restart it. Default it to 0 so as default you do not show the welcome back box.

As for how to change arguments before restarting, please check Modify command line arguments before Application.Restart()

Upvotes: 0

Related Questions