0

How can I make a program that will activate a code when a program/process stated was opened without starting the stated program itself.

Right now I have this code:

while (!IsProcessOpen("ThisProcess")) //"IsProcessOpen" just checks whether the program stated is open in the task manager.
{

}
gopatch.DoWork += new DoWorkEventHandler(gopatch_DoWork);
gopatch.RunWorkerCompleted += new RunWorkerCompletedEventHandler(gopatch_RunWorkerCompleted);
gopatch.RunWorkerAsync();

but this way uses way too much cpu. Is there a way to make it use less cpu but get the job done as I wanted it to?

1 Answers1

1

You could use the Thread.Sleep method in your while loop to prevent excessive CPU consumption. It's not optimal, but it's simple. If you need something better, try using events. This page describes how to use the ManagementEventWatcher to do so.

Community
  • 1
  • 1
Mike Precup
  • 4,148
  • 21
  • 41
  • I would go with hooks and the above code. Let an event handler fire off when a new process starts, and then check to see if the required program is running at that time. http://msdn.microsoft.com/en-us/magazine/cc188966.aspx – Austin T French Jun 21 '13 at 02:32
  • ManagementEventWatcher is the cleanest way to go. – V Maharajh Jun 21 '13 at 03:42