Using the Method Application.Restart() in C# should restart the current Application: but it seems that this is not always working.
Is there a reason for this Issue, can somebody tell me, why it doesn't work all the time?
Using the Method Application.Restart() in C# should restart the current Application: but it seems that this is not always working.
Is there a reason for this Issue, can somebody tell me, why it doesn't work all the time?
Try locking before dumping. Here's how I initiate a full app-dump. Might work for you, might not.
Context.Application.Lock();
Context.Session.Abandon();
Context.Application.RemoveAll();
Context.Application.Restart();
Context.Application.UnLock();
There could be a lot of reasons for this. It's not that the method doesn't work; rather, many times programmers forget that they've put something in their code that would stop the application from automatically shutting down, or starting up. Two examples:
Check your code for gotchas like that. If you're seeing this behaviour within a blank application, then that's more likely to be a problem with the actual function than your code.
The only time I've run into this kind of issue is when in my main form I had a custom FormClosing event handler, that performed logic and canceled the event.
EDIT:
I have now run into another instance and based on your comments it possibly mirrors what you were experiencing. When running a single instance application, using a Mutex, I was calling Application.Restart() from a fairly embedded location, that had a lot of cleanup to do. So it seems the restart was launching a new instance before the previous instance was complete, so the Mutex was keeping the new instance from starting.
// Get the parameters/arguments passed to program if any
string arguments = string.Empty;
string[] args = Environment.GetCommandLineArgs();
for (int i = 1; i < args.Length; i++) // args[0] is always exe path/filename
arguments += args[i] + " ";
// Restart current application, with same arguments/parameters
Application.Exit();
System.Diagnostics.Process.Start(Application.ExecutablePath, arguments);
This seems to work better than Application.Restart();
Not sure how this handles if your program protects against multiple instance. Perhaps this change to the second part would handle that case?:
Application.Exit();
System.Threading.Thread.Sleep(5000);
System.Diagnostics.Process.Start(Application.ExecutablePath, arguments);
My guess is you would be better off launching a second .exe which pauses and then starts your main application for you.
If the application was first launched from a network location and is unsigned (you get the warning dialog first), it won't restart and will only exit.
In my case (NO single-instance), where
Application.Restart();
did not worked,
System.Diagnostics.Process.Start(Application.ExecutablePath);
Application.Exit();
did the job!