Process Checker [Answered]

I'm creating an ""antivirus"" and I'm using a thread to check when a new process is started (but the thread is consuming a lot of cpu and I was wondering if there is another way to do this)
15 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Luizdodibre
Luizdodibre2y ago
@Peep i'm checking every certain time if the process (I'll give an example) notepad.exe was executed if it was it terminates the process however this demands a lot of CPU and I don't know another effective way or that doesn't spend so much cpu
Anchy
Anchy2y ago
share some code as you may be able to do it in a more efficient way
Luizdodibre
Luizdodibre2y ago
internal void exc()
{
if (!hasStarted)
{
hasStarted = true;
var ff = new Form();
int flag = 0;
Thread eventThread = new Thread(() =>
{
while (keepRunning)
{
Thread.Sleep(5);
Process[] ps = Process.GetProcesses();

foreach (Process pr in ps)
{
if (pr.ProcessName.Contains("notepad++"))
{
flag += 1;
try
{
pr.Kill();
if (flag == 1)
{
this.Invoke((MethodInvoker)delegate { ff.Show(); });
}
}
catch (Exception ex)
{
keepRunning = false;
MessageBox.Show($"Oops! {ex}", "Error");
}
}
}
}
});


eventThread.IsBackground = true;
eventThread.Start();
}
}
internal void exc()
{
if (!hasStarted)
{
hasStarted = true;
var ff = new Form();
int flag = 0;
Thread eventThread = new Thread(() =>
{
while (keepRunning)
{
Thread.Sleep(5);
Process[] ps = Process.GetProcesses();

foreach (Process pr in ps)
{
if (pr.ProcessName.Contains("notepad++"))
{
flag += 1;
try
{
pr.Kill();
if (flag == 1)
{
this.Invoke((MethodInvoker)delegate { ff.Show(); });
}
}
catch (Exception ex)
{
keepRunning = false;
MessageBox.Show($"Oops! {ex}", "Error");
}
}
}
}
});


eventThread.IsBackground = true;
eventThread.Start();
}
}
@Anchy the code looks like this
ero
ero2y ago
You need to Dispose all other processes in the array that you don't use
Kouhai
Kouhai2y ago
The app would always be CPU heavy because your loop is running every 5ms
Luizdodibre
Luizdodibre2y ago
@Kouhai yes it is running every 5ms just so the application can't even run (but I don't know any other way to do this without using a thread)
Kouhai
Kouhai2y ago
Unfortunately querying all running proceses is an expensive operation If you're actually interested in how anti viruses work, they essentially get notified when a new process starts instead of looping and checking every process
Luizdodibre
Luizdodibre2y ago
@Kouhai OK. but how can I be ""notified""
Kouhai
Kouhai2y ago
You can take a look at WMI, though a real anti virus would have a custom driver instead of using WMI
Luizdodibre
Luizdodibre2y ago
okay, I'll look
Zendist
Zendist2y ago
Stack Overflow
Is there a System event when processes are created?
Is there any event when a new process is created. I'm writing a c# application that checks for certain processes, but I don't want to write an infinite loop to iterate through all known processes
Zendist
Zendist2y ago
For the WMI approach.
Luizdodibre
Luizdodibre2y ago
Ok thanks
Accord
Accord2y ago
✅ This post has been marked as answered!