1

Okay so ive got another one, Right now im working on a button, called installbtn in code.

Right now i have in my code.

private void installbtn_Click(object sender, EventArgs e)
        {
// Access the internal exe resource pcc - the pcc is Path Copy Copy, which i am using as a
// requirement to use this software
   byte[] pccFile = Properties.Resources.pcc;  

// The resource pcc.exe as a binary called pcc which is then used as a byte called pccFile  
   string pccExe = Path.Combine(Path.GetTempPath(), "pcc.exe");  
// The Executable and its filename + Extenstion
   using (FileStream exeFile = new FileStream(pccExe, FileMode.Create))
   exeFile.Write(pccFile, 0, pccFile.Length);
// Write the file to users temp dir
   Process.Start(pccExe);

// Start the Installer
   installbtn.Text = "Installing...";
   StatusLabel1.Text = "Installing PCC Now...";
// Indicate on the form, the current process status.


// Here i want the application to check if pccExe has closed
// after the user has installed the component and its process "pcc.exe" exits
   MessageBox.Show("Module Installed /r/nPlease Start the Application", "Application Module Installed");
   installbtn.Text = "Restart Now!";
   StatusLabel1.Text = "Please Restart the Application";
// and if it has then show a message box and reflect in the form
// i want it to quit and restart the application after the message box is closed
}

now when the installer finishes, i would like to be able to detect when the installer closes ("pcc.exe") after install and then reload the application once it has. Not sure if its possible but i would appreciate the help.

Thanks Shaun.

MasterCassidy
  • 49
  • 1
  • 14
  • Process has an `Exited` event that can be used – Sayse Sep 01 '14 at 09:17
  • Check this two links: 1 - http://stackoverflow.com/questions/3147911/wait-till-a-process-ends; 2 - http://stackoverflow.com/questions/6390030/c-sharp-making-a-process-start-wait-until-the-process-has-start-up – Jorge Oliveira Sep 01 '14 at 09:34

2 Answers2

4

You could use Process.WaitForExit()

The WaitForExit() overload is used to make the current thread wait until the associated process terminates. This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;

startInfo.FileName = exeToRun;    
process.Start();
process.WaitForExit();
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • I have put in a try catch block and its giving me the message, No process associated with the object. I am trying to target the win32 process from a 64 machine. after that, it proceeds to run the rest of the code as normal. as whats happening is that when it launches the internal exe stored in the byte is being launched but then it needs to wait till the programme and process has closed normally. i can make the source available since it is freeware im making. – MasterCassidy Sep 02 '14 at 07:47
  • 1
    @Shaun'Shorty'Cassidy Just did a minor change to the code, call **process.Start()** before calling **process.WaitForExit()** – Kurubaran Sep 02 '14 at 08:04
  • Yeah that worked out, its all good now thanks Kurubaran ill make sure to credit you in my source code. – MasterCassidy Sep 02 '14 at 09:54
0

Start returns a Process object, on which you can wait (or start a thread that waits, etc.).

Medinoc
  • 6,577
  • 20
  • 42