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?
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.
Check Microsoft's sourcecode of application restart.
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.
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.
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();
I Tried with below code and it is working fine
static class Program
{
static Mutex _mutex = new Mutex(false, "MYAPP");
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!_mutex.WaitOne(1000, false))
{
MessageBox.Show("Another instance is already running!!!", "Already Running", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PrimaryForm());
_mutex.ReleaseMutex();
}
}
//the place where i am calling application restart used this code
Application.Restart();
Application.ExitThread();
Reference Link
https://www.codeproject.com/articles/25674/preventing-multiple-application-instances-when-usi
I have this very same issue with .Net 4.7 framework. The accepted answer was key for my success. I did had code in the FormClosing event that was taking some time and stopping the restart process. What I did was to put a sentinel like this:
If JustCloseIT = False Then
'all closing code, like logging the session log-out to a database and all those goodies we all do.
End If
only then the Application.Restart() worked!