2

I am developing a Windows Forms application in which I need to edit certain config files. Now when the user clicks on the edit option, I want to launch these config files in a simple notepad editor. Once launched I want to stall my application. Only when the user closes the notepad editor, I want to un-stall my application. How can this be done ?

I have seen these questions, but the answers have many issues. (I read the comments given there.)

Q1Q2

Community
  • 1
  • 1
Akash Deshpande
  • 2,583
  • 10
  • 41
  • 82
  • 1
    Please take a look at the question: [Visual C# GUI stops responding when process.WaitForExit(); is used](http://stackoverflow.com/questions/1728099/visual-c-sharp-gui-stops-responding-when-process-waitforexit-is-used). – Sergey Vyacheslavovich Brunov Oct 03 '13 at 07:36

3 Answers3

3

You can use the Exited-Event:

try
{
     Process myProcess = new Process();
     myProcess.StartInfo.FileName = "notepad.exe";
     myProcess.StartInfo.Arguments = @"C:\PathToYourFile";
     myProcess.EnableRaisingEvents = true;
     myProcess.Exited += new EventHandler(myProcess_Exited);
     myProcess.Start();
 }
 catch (Exception ex)
 {
     //Handle ERROR
     return;
 }

// Handle Exited event and display process information. 
private void myProcess_Exited(object sender, System.EventArgs e)
{
     eventHandled = true;
     Console.WriteLine("Exit time:    {0}\r\n" +
            "Exit code:    {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
}
makim
  • 3,154
  • 4
  • 30
  • 49
3
var process = System.Diagnostics.Process.Start("yourfile.txt");
process.WaitForExit();

This will open your file. However, sometimes the process will be null and you will not be able to wait for exit. Why?

Process.Start Method

Return Value Type: System.Diagnostics.Process

A new Process component that is associated with the process resource, or null if no process resource is started (for example, if an existing process is reused).

http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

Community
  • 1
  • 1
Gusdor
  • 14,001
  • 2
  • 52
  • 64
2

You can initiate a Process and WaitForExit:

Process pr = Process.Start("Notepad");
pr.WaitForExit();
Alireza
  • 10,237
  • 6
  • 43
  • 59