C
C#3y ago
kuba_z2

Process.EnableRaisingEvents does not work on Windows

I have an app that have 20 workers that are other processes. On production, on ubuntu, when I close the main process with ^C, the workers die as they should, but on Windows, when I kill the process, the workers still exist and use 100% of my i9-12900k (lol), it's because the workers have endless loops and throw exceptions in them when there is no main process. When I close app console window, the main process and workers are not closed and when I kill the main process, I have 100% of cpu usage. Did I miss something? I use this method to create a worker:
private Process CreateProcess()
{
var process = new Process()
{
StartInfo = new("ScriptingService", _arguments),
EnableRaisingEvents = true,
};
process.Start();
return process;
}
private Process CreateProcess()
{
var process = new Process()
{
StartInfo = new("ScriptingService", _arguments),
EnableRaisingEvents = true,
};
process.Start();
return process;
}
3 Replies
jcotton42
jcotton423y ago
The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited.
this just allows the parent to know when the child has exited how are the child processes told to exit?
kuba_z2
kuba_z2OP3y ago
oh, so I misunderstood it (actually found a response on StackOverflow that told EnableRaisingEvents makes process die after main process is closed some time ago). Is there any easy way to make child processes die after main process is closed?
Kai
Kai3y ago
I don't think so, you could automatically do this when you close though, on winforms/WPF you get an event. In a console app you'd have to somehow put a big try-catch around everything at the end + AppDomain.CurrentDomain.UnhandledException and kill all child processes that way?
Want results from more Discord servers?
Add your server