• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > What's equivalent in WPF for "Application.Restart()" in WindowsForms?
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerWhat's equivalent in WPF for "Application.Restart()" in WindowsForms?

  • Tuesday, September 19, 2006 12:56 PMCyber Sinh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Application.Curent object doesn't contain such method.
    Any suggestions?

    Thanks
    • ReplyReply
    • QuoteQuote
     

Answers

  • Tuesday, September 19, 2006 5:06 PMAshish Shetty - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0
    Afaik, there isn't one in WPF.
    • ReplyReply
    • QuoteQuote
     

All Replies

  • Tuesday, September 19, 2006 5:37 PMCyber Sinh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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

    • ReplyReply
    • QuoteQuote
     
  • Thursday, March 15, 2007 4:10 PMnickbaker77 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Is there going to be one? Or equivalent functionality? How are you supposed to 'refresh' the application following a ClickOnce update?

    Thanks

    Nick
    • ReplyReply
    • QuoteQuote
     
  • Friday, March 16, 2007 12:04 AMChango V. - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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. ;-)
    • ReplyReply
    • QuoteQuote
     
  • Tuesday, November 06, 2007 8:51 AMAlexVallat Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement