Click here to Skip to main content
15,891,597 members
Home / Discussions / C#
   

C#

 
GeneralRe: Automatically Install Updated Application Version Pin
PDTUM30-Aug-10 3:56
PDTUM30-Aug-10 3:56 
GeneralRe: Automatically Install Updated Application Version Pin
Sunil G30-Aug-10 18:13
Sunil G30-Aug-10 18:13 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein30-Aug-10 14:38
Matthew Klein30-Aug-10 14:38 
GeneralRe: Automatically Install Updated Application Version Pin
Luc Pattyn30-Aug-10 14:54
sitebuilderLuc Pattyn30-Aug-10 14:54 
GeneralRe: Automatically Install Updated Application Version Pin
Chuck O'Toole30-Aug-10 16:16
Chuck O'Toole30-Aug-10 16:16 
GeneralRe: Automatically Install Updated Application Version Pin
PDTUM30-Aug-10 16:51
PDTUM30-Aug-10 16:51 
GeneralRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 4:44
Matthew Klein31-Aug-10 4:44 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 8:27
Matthew Klein31-Aug-10 8:27 
Something else I do to deal with the possible race condition that you're talking about is I pass the Process ID of the current (old) exe to the new exe via arguments.

Here is a slightly modified version of what I posted earlier:
void UpdateAndRestart()
 {
     //Assumes you have already downloaded new exe to myNewExeLoc
     string myNewExeLoc = @"C:\My Folder\myNewExe.new";
     string myCurrentExe = System.Reflection.Assembly.GetExecutingAssembly().Location;
     string myBakExe = myCurrentExe + ".bak";
     if (System.IO.File.Exists(myBakExe))
         System.IO.File.Delete(myBakExe);
     System.IO.File.Move(myCurrentExe, myBakExe);
     System.IO.File.Move(myNewExeLoc, myCurrentExe);
     System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess(); //Change #1
     System.Diagnostics.Process newP = new System.Diagnostics.Process();
     newP.StartInfo.FileName = myCurrentExe;
     newP.StartInfo.Arguments = string.Format("/restart {0}", p.Id); //Change #2
     newP.Start(); //Make sure this is the second to last thing you do
     System.Environment.Exit(0); //Make sure this is the last thing you do
 }
 //Then do this in your Main (or Splash) but do it before you need to muck with the new/old exe thing
 void Main(string[] args)
 {
     if (args.Length > 0)
     {
         switch (args[0].ToLower())
         {
             case "/restart":
                 int oldPid;

                 if (args.GetUpperBound(0) < 1 || !int.TryParse(args[1], out oldPid))
                     break;

                 Process p = System.Diagnostics.Process.GetProcessById(oldPid);
                 if (p != null)
                     p.Kill();   //since you already did all your shutdown stuff before you called this
                                 //it should be safe to just kill the process if its hanging
                 break;
         }
     }
 }

GeneralRe: Automatically Install Updated Application Version Pin
PDTUM31-Aug-10 9:24
PDTUM31-Aug-10 9:24 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 10:24
Matthew Klein31-Aug-10 10:24 
QuestionCOM PORT SETTING THROUGH C# IDE Pin
venkat swaminathan29-Aug-10 7:40
venkat swaminathan29-Aug-10 7:40 
QuestionUIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:19
zhiyuan1629-Aug-10 5:19 
AnswerRe: UIS Application\MainDialog.Designer.cs Pin
Luc Pattyn29-Aug-10 5:24
sitebuilderLuc Pattyn29-Aug-10 5:24 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:31
zhiyuan1629-Aug-10 5:31 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Dan Mos29-Aug-10 5:41
Dan Mos29-Aug-10 5:41 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:45
zhiyuan1629-Aug-10 5:45 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Dan Mos29-Aug-10 6:02
Dan Mos29-Aug-10 6:02 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Luc Pattyn29-Aug-10 5:43
sitebuilderLuc Pattyn29-Aug-10 5:43 
QuestionA simple question. Persistence block in C# text editor? Pin
Fred 3429-Aug-10 3:50
Fred 3429-Aug-10 3:50 
AnswerRe: A simple question. Persistence block in C# text editor? Pin
Abhinav S29-Aug-10 4:10
Abhinav S29-Aug-10 4:10 
GeneralRe: A simple question. Persistence block in C# text editor? Pin
Fred 3429-Aug-10 23:06
Fred 3429-Aug-10 23:06 
AnswerRe: A simple question. Persistence block in C# text editor? Pin
PIEBALDconsult29-Aug-10 5:18
mvePIEBALDconsult29-Aug-10 5:18 
Questiongetting "attempted to read or write protected memory in .net" when trying to create a AVI file. Pin
Member 425763228-Aug-10 20:36
Member 425763228-Aug-10 20:36 
AnswerRe: getting "attempted to read or write protected memory in .net" when trying to create a AVI file. Pin
Luc Pattyn29-Aug-10 1:46
sitebuilderLuc Pattyn29-Aug-10 1:46 
QuestionC# loading data Pin
C.CoderCreator28-Aug-10 16:23
C.CoderCreator28-Aug-10 16:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.