MADMap
MADMap

Reputation: 3192

Why is Application.Restart() not reliable?

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?

Upvotes: 49

Views: 68787

Answers (12)

Martin.Martinsson
Martin.Martinsson

Reputation: 2154

Extension methods:

public delegate void MethodDelegate<in TControl>(TControl value);

public static void InvokeIfRequired<TControl>(this TControl control, MethodDelegate<TControl> action)
    where TControl : Control
{
    if (control.InvokeRequired)
    {
        control.Invoke(action, control);
    }
    else
    {
        action(control);
    }
}  

Class privates:

private static bool _exiting;
private static readonly object SynchObj = new object();

Working horse:

public static void ApplicationRestart(params string[] commandLine)
{
    lock (SynchObj)
    {
        if (Assembly.GetEntryAssembly() == null)
        {
            throw new NotSupportedException("RestartNotSupported");
        }

        if (_exiting)
            return;

        _exiting = true;

        if (Environment.OSVersion.Version.Major < 6) return;

        bool cancelExit = true;

        try
        {
            foreach (Form f in Application.OpenForms.OfType<Form>().ToList())
            {
                f.InvokeIfRequired(frm =>
                {
                    frm.FormClosing += (sender, args) => cancelExit = args.Cancel;
                    frm.Close();
                });

                if (cancelExit) break;
            }

            if (cancelExit) return;

            Process.Start(new ProcessStartInfo
            {
                UseShellExecute = true,
                WorkingDirectory = Environment.CurrentDirectory,
                FileName = Application.ExecutablePath,
                Arguments = commandLine.Length > 0 ? string.Join(" ", commandLine) : string.Empty
            });

            Application.Exit();
        }
        finally
        {
            _exiting = false;
        }
    }
}

Upvotes: 1

Ashutosh Ranjan
Ashutosh Ranjan

Reputation: 29

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

Upvotes: 1

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!

Upvotes: 0

TheSmurf
TheSmurf

Reputation: 15568

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.

Check Microsoft's sourcecode of application restart.

Upvotes: 31

cmptrs4now
cmptrs4now

Reputation: 69

In my program I have a mutex to ensure only one instance of the application running on a computer. This was causing the newly started application to not start because the mutex had not been release in a timely fashion. As a result I put a value into Properties.Settings that indicates that the application is restarting. Before calling Application.Restart() the Properties.Settings value is set to true. In Program.Main() I also added a check for that specific property.settings value so that when true it is reset to false and there is a Thread.Sleep(3000);

In your program you may have the logic:

if (ShouldRestartApp)
{
   Properties.Settings.Default.IsRestarting = true;
   Properties.Settings.Default.Save();
   Application.Restart();
}

In Program.Main()

[STAThread]
static void Main()
{
   Mutex runOnce = null;

   if (Properties.Settings.Default.IsRestarting)
   {
      Properties.Settings.Default.IsRestarting = false;
      Properties.Settings.Default.Save();
      Thread.Sleep(3000);
   }

   try
   {
      runOnce = new Mutex(true, "SOME_MUTEX_NAME");

      if (runOnce.WaitOne(TimeSpan.Zero))
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
      }
   }
   finally
   {
      if (null != runOnce)
         runOnce.Close();
   }
}

That's it.

Upvotes: 6

Nathan
Nathan

Reputation: 889

I know this is an old thread, but I found a workaround. Hopefully this will help someone else in need.

I needed a solution that would trigger the update sequence during a ClickOnce Application startup from code. Applicatoin.Restart did not do this. I wanted a way of being able to check for an update and then invoking the built in update manager so that I didn't have to write a custom one.

    'VB Code Sample
    Dim strStart As String = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\Programs\Folder\YourApplication.appref-ms"
    Application.Exit()
    Try
        Process.Start(strStart)
    Catch ex As Exception
        'Do something with the exception
    End Try

The only issue that I see with this workaround is that a user could delete the shortcut from the start menu. If that is a concern you could write some code to copy the start menu link to the some folder of your choosing, preferably in the ClickOnce application folder. This is important because the start menu icon for your application is not a .lnk or .exe, it is actually a .appref-ms link. See ClickOnce .appref-ms more than a link to .application file? This link explains this in more detail.

This code will work with ClickOnce SingleInstance Applications.

Upvotes: -1

Timothy Carter
Timothy Carter

Reputation: 15785

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.

Upvotes: 7

cprcrack
cprcrack

Reputation: 19110

In my case (NO single-instance), where

Application.Restart();

didn't work,

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

did the job!

Upvotes: 21

shima lagzian
shima lagzian

Reputation: 19

Application.Restart();
Application.ExitThread();                                                     

this worked for me thanks.

Upvotes: 0

ferhatayhan
ferhatayhan

Reputation: 49

Try this code:

bool appNotRestarted = true;

This code must also be in the function:

if (appNotRestarted == true)
{
    appNotRestarted = false;
    Application.Restart();
    Application.ExitThread();
}

Upvotes: 0

Matt Hanson
Matt Hanson

Reputation: 3504

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.

Upvotes: 1

Oli
Oli

Reputation: 239810

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();

Upvotes: 2

Related Questions