Le réseau pour les développeurs > Forums - Accueil > Windows Presentation Foundation (WPF) > What's equivalent in WPF for "Application.Restart()" in WindowsForms?
Poser une questionPoser une question
 

TraitéeWhat's equivalent in WPF for "Application.Restart()" in WindowsForms?

Réponses

Toutes les réponses

  • mardi 19 septembre 2006 17:06Ashish Shetty - MSFTModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Afaik, there isn't one in WPF.
  • mardi 19 septembre 2006 17:37Cyber Sinh Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Why a such method has been omitted in WPF?
    It's very useful for ClickOnce (after an update) or a language change...
    There is absolutly no alternative?

    Thanks

  • jeudi 15 mars 2007 16:10nickbaker77 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Is there going to be one? Or equivalent functionality? How are you supposed to 'refresh' the application following a ClickOnce update?

    Thanks

    Nick
  • vendredi 16 mars 2007 00:04Chango V. - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    This feature may be added later on, but Application.Restart() is not hard to implement for your application. Use Reflector to see the WinForms implementation. ;-)
  • mardi 6 novembre 2007 08:51AlexVallat Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    As it turns out, Application.Restart() is quite hard to implement properly, as evidenced by using Reflector to inspect the WinForms implementation and discovering that it isn't actually implemented properly there, for a start. It completely ignores command line escaping when creating the command line to start the new process with. To demonstrate this, you can use some very short code just to show command args and perform a restart:

            static void Main(string[] args)
            {
                Console.WriteLine("Restart Tester");
                Console.WriteLine("command line: " + Environment.CommandLine);
                Console.WriteLine("args:");
                foreach (string arg in args)
                    Console.WriteLine("\t" + arg);
                Console.WriteLine();
                Console.WriteLine("Enter r to restart, or anything else to quit");
                if(Console.ReadLine() == "r")
                {
                    Application.Restart();
                }
            }


    Open up a command prompt, and run: RestartTest.exe "te\" st" Due to the (admittedly obscure) command line escaping rules, the result of this is:

    command line: RestartTest.exe "te\" st"
    args:
            te" st

    If the application is then restarted using Application Restart, the escaping is ignored, and the restarted instance displays:

    command line: "C:\...\RestartTest.exe" "te" st"
    args:
            te
            st

    If you want to do Restart properly, it's substantially trickier to get the command line to pass to the next instance. My current code for doing this, which I hope handles all cases, is:

            /// <summary>
            /// Gets the command args this app was started with, as executed, not parsed into args[]
            /// </summary>
            public static string GetCommandArgs()
            {
                string commandLine = Environment.CommandLine;

                //Find either the space, which is a the delimeter, or a " mark, which bounds spaces
                int pos = 0;
                do
                {
                    if (pos >= commandLine.Length)
                        return String.Empty; //No command line args

                    pos = commandLine.IndexOfAny(new char[] { ' ', '"' }, pos);
                    if (pos == -1)
                        return String.Empty; //No command line args

                    if (commandLine[pos] == '"')
                    {
                        //Find the closing " mark. " marks can't be escaped in path names
                        pos = commandLine.IndexOf('"', pos + 1) + 1;
                        if (pos == 0)
                        {
                            //No command line args. Probably malformed command line too.
                            System.Diagnostics.Trace.TraceWarning("Could not find closing \" mark in command line: " + commandLine);
                            return String.Empty;
                        }
                        //Otherwise, go round again to find another quote, or alternatively a space
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(commandLine[pos] == ' ', "Expecting a space here, if not a \" mark");
                        //Everything past this point should now be the command args, as this is an unquoted space
                        return commandLine.Substring(pos + 1);
                    }
                } while (true);
            }

    I hope this helps, and if anyone has further refinements on the above, or notices cases for which it does not work, I'd be interested to hear them.

    Alex
  • jeudi 23 juillet 2009 18:50Junior Mayhe Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposéeA du code
    As far as I know you could add a reference to System.Windows.Forms and add these code statements:


    System.Windows.Forms.Application.Restart();
    Application.Current.Shutdown();
    

    • Proposé comme réponseJunior Mayhe jeudi 23 juillet 2009 18:50
    •  
  • vendredi 2 octobre 2009 10:17Michael SyncMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    IMO, We should never reference to WIndows.Form.

    Better to use the code like that below ....

    System.Diagnostics.

    Process.Start(Application.ResourceAssembly.Location);
    Application.Current.Shutdown();


    Michael Sync: blog: http://michaelsync.net