C
C#Luizdodibre

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)
UU
Unknown User553d ago
L
Luizdodibre553d 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
A
Anchy553d ago
share some code as you may be able to do it in a more efficient way
L
Luizdodibre553d 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
E
ero553d ago
You need to Dispose all other processes in the array that you don't use
K
Kouhai /人◕ ‿‿ ◕人\553d ago
The app would always be CPU heavy because your loop is running every 5ms
L
Luizdodibre553d 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)
K
Kouhai /人◕ ‿‿ ◕人\553d 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
L
Luizdodibre551d ago
@Kouhai OK. but how can I be ""notified""
K
Kouhai /人◕ ‿‿ ◕人\551d ago
You can take a look at WMI, though a real anti virus would have a custom driver instead of using WMI
L
Luizdodibre551d ago
okay, I'll look
Z
Zendist551d 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
Z
Zendist551d ago
For the WMI approach.
L
Luizdodibre551d ago
Ok thanks
A
Accord544d ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
ArgumentNullException when argument is not nullI have this code: ```cs public class Config { [JsonProperty("token")] public string Token = "TOKWPF reusing the same template across multiple columnsThe following question https://social.msdn.microsoft.com/Forums/silverlight/en-US/f84e0efd-b1f2-4ab6Cascading DropDownI'm using MVC (.NET5) with EF and had this code to select a species on a View (to Create/Edit): ```cAssembly service findingHi, I am trying to find out if it's possible to get IServiceProvider, Microsoft IConfiguration or sohow do i submit a progra min vs to google classroomim trying to submit a project that's due to today but when i do submit it all i get is this and not WPF custom property not recognized by the compilerI have defined a custom property like below, it's in the namespace `CarApp`. It is supposed to repreEntity Framework DbContextOptionsBuilder.UseSqlServer() missing definitionI just installed the newest version of Entity Framework (6.0.9) and am inside my program.cs trying tDoes API manual nuget downloads come with the package dependencies?-WPF passing binding to a template from DataGridTemplateColumnSay, I have a template like this in resources, which I want to reuse for multiple columns, and whichBest way to implement a regex based lexer [Answered]In a regex lexer, you can loop over every pattern and do something like this: ```cs if (match.Successtring query in C Sharpi am trying to get values from 2 website links and assign them to a lable using visual studio one is there a convention for validating login sessions with MVC?Assuming I'm using ASP, ADO and .NET Say I want to construct a project to have several functions thUsing SQL db to create object ID or application?I have an application where my records in my DB need to have unique IDs. Should I leave this responsObject Initializers - To Parenthesize or Not To Parenthesize (that is the question)What is the difference between the object initializers on lines 1 and 2? In what situations should oHow do I move my mouse curser on screen?I went through most of stack overflow and some YouTube tutorials but didn't fins anythingHow do I move my mouse curser on screen?i was wondering if its possible to move the curse on the screen using c#SQL Error when trying to get an IDI'm trying to add username password and email to my users table And then get the ID of this new userEF Structuring ProblemBasically I have users that I want to assign to a group. But I also want the group to have a parent,data transfer between classesI want to list 'uname' value in method X, to method Y i am new to oopA generic and efficient way to feed in text for a lexer or scannerIn the past I was lazy and always just shoved a string into my lexical analyzer as input, but what's