Reputation: 37662
I have got a few pairs of the identical apps.
It looks like
Pair 1: App A + App B
Pair 2: App A + App B
and etc
Each App A starts App B from its folder. (They are both at the same folder.)
So I start it like this
Code of App A
Process p = new Process();
p.StartInfo.FileName = FileManager.AppDirectoryName + "\\" + AppB;
p.Start();
And I also stop App B like this.
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == AppB)
{
p.Kill();
return;
}
}
The problem is that at the same time could be many App B executing so this method does not allow to detect target App B to kill.
I assume to use PID of App B to kill it. But I don't know how to obtain PID at the moment to start App B...
Any clue?
Upvotes: 1
Views: 225
Reputation: 1663
Save a reference to the Process object:
Process p = new Process();
p.StartInfo.FileName = @"c:\windows\notepad.exe";
p.Start();
// ...
p.Kill();
or remember its PID:
Process p = new Process();
p.StartInfo.FileName = @"c:\windows\notepad.exe";
p.Start();
long pid = p.Id;
// ...
Process.GetProcessById(pid).Kill();
Upvotes: 1
Reputation: 3363
After starting the app, the Id property on the Process object will indicate the PID of the newly started process
var PID = p.Id;
Upvotes: 3