2

I am really bad at explaining, but what I want to do is make it so my program closes once it detects that another program closes.

I was thinking making a while statement, like:

   Process thisProc = Process.GetCurrentProcess();
   while(IsProcessOpen("chrome.exe") == false)
   this.Close();

but then I notice that it was a "while", not something that detects once it happens, it executes the code once, and the code above would execute it while my chrome is closed.

I also want to know how to open chrome when this is open when something is selected, such as

   while(button4.Backcolor == Color.Lime); --acts like a checkbox
   Process.Start("Chrome.exe");

However, I do not want the Backcolor to change to red once Chrome opens Also, this method will keep opening chrome while the backcolor is lime(acts as a signal)

Andly Kwan
  • 61
  • 1
  • 7
  • 1
    Use the exit event: https://stackoverflow.com/a/9432911/3165499 – Ken Hung Apr 18 '20 at 13:44
  • Take a look at this https://stackoverflow.com/questions/36629764/c-sharp-monitor-external-process-state – ipinak Apr 18 '20 at 13:45
  • 2
    Does this answer your question? [C# detect process exit](https://stackoverflow.com/questions/9432870/c-sharp-detect-process-exit) – Youssef13 Apr 18 '20 at 13:45
  • _"However, I do not want the Backcolor to change to red once Chrome opens"_ - huh? There's no evidence that such a thing will happen. – ProgrammingLlama Apr 18 '20 at 13:58
  • think about events or tasks – Iria Apr 18 '20 at 13:59
  • ipinak I want to make it so it executes the code once ||| John, someone told me to use the color of a button as a custom checkbox, and said I should use it to trigger whether or not the code executes, but I want it so it is always on – Andly Kwan Apr 18 '20 at 14:26

1 Answers1

0

you have to add some dummy after the while command, like this

Process thisProc = Process.GetCurrentProcess();
while(IsProcessOpen("chrome.exe") == false)
{
}
this.Close();
Ohnemichel
  • 324
  • 2
  • 9
  • 1
    the {} do nothing, but they protect this.Close() from being executed DURING the while loop. a different Possibility would be to write ; after the while loop. – Ohnemichel Apr 18 '20 at 13:54