This program will kill the process a user specifies and start a BackgroundWorker that constantly analyzes a list of running processes and kills it again if it starts. But it will not cancel even when I tell it to.
This is the BackgroundWorker's code.
private void KeepProcessKilled_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
foreach (Process process in Process.GetProcesses())
{
if (process.ProcessName == BadProcessName)
{
try
{
process.Kill();
}
catch
{
}
}
}
}
}
And the code that stops it.
private void DeleteButton_Click(object sender, EventArgs e)
{
KeepProcessKilled.CancelAsync();
ResultsBox.AppendText("KeepProcessKilled procedure has stopped.");
}
The process continues to use 100 percent of the CPU and slows the system even after it is supposed to. I'm guessing I need to have a break somewhere in the while loop but I'm not sure where to put it or how to get to it when it's supposed to cancel.
Also is there a way to limit the CPU usage? 100 percent seems like a bit much for just checking what processes are running.