So I'm building a .NET core 3.1 app and I am opening a few processes, I want to keep track of them and kill them when I click certain buttons, however.. I was reading that killing a process based on an ID was not a good idea.
So I found this SO post about it
https://stackoverflow.com/a/2316683/11966121
and it was explaining how to do it, but that uses the kernel32 dll which doesnt exist in Linux, so what can I do to convert it?
I essentially want to do this, but in Linux.
class Job
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string lpName);
[DllImport("kernel32.dll")]
public static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess);
[DllImport("kernel32.dll")]
public static extern bool TerminateJobObject(IntPtr hJob, uint uExitCode);
IntPtr job;
public Process StartProc(string commandLine)
{
if (job == IntPtr.Zero)
job = CreateJobObject(IntPtr.Zero, null);
ProcessStartInfo si = new ProcessStartInfo(@"c:\windows\system32\cmd.exe");
si.Arguments = "/c " + commandLine;
si.CreateNoWindow = false;
si.UseShellExecute = false;
Process proc = Process.Start(si);
AssignProcessToJobObject(job, proc.Handle);
return proc;
}
public void TerminateProc()
{
// terminate the Job object, which kills all processes within it
if (job != null)
TerminateJobObject(job, 0);
job = IntPtr.Zero;
}
}