I am creating an application in C# for monitoring some applications, so how to know when an exe running (ex: notepad).
I tried using
timer
run checkif (Process.GetProcessesByName("process_name").Length > 0)
every 1s but maybe not the good way.
Asked
Active
Viewed 76 times
0

JimmyN
- 579
- 4
- 16
-
what exactly you want to know ? That the given application(say Notepad) is currently running or not ? or you want to know the time of its execution since you tried using Timer – Nitin Singh Oct 11 '17 at 07:14
-
i want know when notepad start, i tried using if (Process.GetProcessesByName("process_name").Length > 0) every 1s. – JimmyN Oct 11 '17 at 07:17
-
then you already did the right thing – BugFinder Oct 11 '17 at 07:18
-
this can be simply done by using a infinite while loop. see my updated answer below. – Nitin Singh Oct 11 '17 at 07:19
2 Answers
0
I think best approach would be to set timer which will check for running processes every, let's say, 5 seconds. In timer Tick
event, check for running process by it's name
// This will return an empty array if notepad isn't running.
Process[] localByName = Process.GetProcessesByName("notepad");
take a look at MSDN documentation

Nino
- 6,931
- 2
- 27
- 42
0
i'm building an application same for u. It's is my solution:
public void findProcess(string processName)
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += (object sender, DoWorkEventArgs e){
while(true)
{
Process[] process = Process.GetProcesses();
foreach(var p in process )
if(p.ProcessName.Equals(processName))
{
// TODO:
}
Thread.Sleep(1000);
}
};
}

Cát Bụi
- 1
- 5