0

I need help finding a way to make my program do an action only when a certain other program is running.

I have tried a while loop

While process.getprocessbyname("process name").Length != 0
{
     //what i want to execute
}

I want my program to run an action only once when a certain process is running. This question is not about finding how I can know if a process is running because I already know how to do so but in fact I am trying to execute a action when a process is running.

Dread415
  • 31
  • 8
  • https://stackoverflow.com/questions/262280/how-can-i-know-if-a-process-is-running – Vytautas Plečkaitis Jul 18 '19 at 16:58
  • Add timer, that checks if there's process by name. If yes - do something, if not - do nothing. – Vytautas Plečkaitis Jul 18 '19 at 16:59
  • i know how to check if a proccess is running – Dread415 Jul 18 '19 at 17:01
  • lemme try the timer – Dread415 Jul 18 '19 at 17:01
  • 3
    `while (Process.GetProcessesByName("processName").Length == 0);` will pause your program until the process is running. Then you can do `//what I want to execute` on the next line. But using a timer is probably better if your program should be doing other things while you wait. – Rufus L Jul 18 '19 at 17:01
  • but will it constantly do ```//what i want to execute``` alot of times if the process is open? – Dread415 Jul 18 '19 at 17:03
  • 1
    No, the semi-colon at the end of the `while` statement means that it will just churn on that line until the condition is `false` (which is when the process is running). Then the next line of code will run (one time). – Rufus L Jul 18 '19 at 17:04
  • Please put that as a answer so i can upvote – Dread415 Jul 18 '19 at 17:05
  • 1
    You're welcome to answer it yourself if you want, since I don't really like that one-liner and don't have time to write a proper answer right now. :) – Rufus L Jul 18 '19 at 17:06
  • 1
    Even without needing your program to process while waiting, it probably wouldn't be a bad idea to put a `Thread.Sleep(100)` as the `while` body so your program doesn't tie up the PC scanning for the process to be running. – NetMage Jul 18 '19 at 21:19

0 Answers0