Search code examples
c#.netwindowsprocess

How to stop process in C#, knowing its filename?


I have a program that runs another one (let's call the first app Stater and the second app - Worker).

I use:

process.start();
process.waiForExit();
process.Close();

in Starter.

But, if Starter is forced to close while waiting for Worker (for some extern reason) Worker will be still in processes, blocking files, eating memory etc.

So, I want to check if Worker is already running. Before, I will try to start it.

I've tried Process.GetProcessesByName("worker.exe") but no luck (even if I can see Worker in Task Manager).

I've seen some topics here about checking every process in memory for its modules, but still, I already know the running file I hope to avoid such solution.

Any advice?


Solution

  • At the time of starting Worker process, save its ID in your application's configuration/setting file, in this way when you will launch your Starter process, it will first load that ID from settings file and will check if that process is currently running or not. If you want to immediately close Worker process, you can call process.Kill() on it.