tags:

views:

4691

answers:

6

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?

A: 

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();
Oli
why is this no good?
sweeney
I guess it didn't work for them. Nice of them to have left a comment if that was the case.
Oli
well i'm trying it - will post back...
sweeney
Context is for aspx apps, right?
sweeney
+8  A: 

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:

  • The Closing event on a form can stop an app's shutdown
  • If you're doing checking for an already-running process, the old one may not be closing fast enough to allow the new one to start up.

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.

DannySmurf
I AM using an SingleInstance-Application, but I didn't think that this can cause the problem. I rather thought .NET is smart enough to let the Process close till the next instance will start.
MADMap
+3  A: 

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.

Timothy Carter
A: 

Start/Exit Method

// 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.

Adam Nofsinger
This will not work. Application.Exit() sets just a flag, which will et the app close somewhere in the messageloop, the sleep if of no use here. Even if, you cannot expect to *terminate* an application and let the *terminated* application do any actions for you afterwards like stating a new process..
codymanix
+1  A: 

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.

Matt Hanson
A: 

In my case (NO single-instance), where

Application.Restart();

did not worked,

System.Diagnostics.Process.Start(Application.ExecutablePath);
Application.Exit();

did the job!

cprcrack