Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone this is probably my last question on here as i've learnt a lot from this site regarding vb.net, there's just one thing i can seem to figure out. My filesystemwatcher uses like 50 percent cpu usage and i was wondering if anyone could edit my code to change the usage (lower cpu useage) also tell me why it's so high. Thanks

heres code, Remember this is just one even ive got any event for renamed, created, changed, delted :::

Private Sub FileSystemWatcher1_Renamed(ByVal sender As System.Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
       Dim OpenFileDialog3 As String
       Try
           Detect.Labellastreal.Text = e.FullPath
           ListBox3.Items.Add(Detect.Labellastreal.Text)
           OpenFileDialog3 = ""


           Dim md5 As New MD5CryptoServiceProvider
           Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
           md5.ComputeHash(f)
           Dim hash As Byte() = md5.Hash
           Dim buff As New StringBuilder
           Dim hashByte As Byte
           For Each hashByte In hash
               buff.Append(String.Format("{0:X2}", hashByte))
           Next
           f.Close()
           If My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Database\UnwantedProgs.txt").Contains(buff.ToString) Then
               OpenFileDialog3 = e.FullPath
               File.Move(Detect.Labellastreal.Text, Path.Combine("C:\ProgramData\DreamyTS\Infected\", Path.GetFileName(Detect.Labellastreal.Text)))
               Detect.ShowDialog()

               WriteToLog("Unwanted Program detected")
           End If

       Catch exception1 As Exception
           ProjectData.SetProjectError(exception1)
           Dim ex As Exception = exception1
           ProjectData.ClearProjectError()
       End Try
   End Sub


It all works just high cpu useage

What I have tried:

im not really sure what to try to be honest...
Posted
Updated 2-Nov-17 10:17am
Comments
Mehdi Gholam 2-Nov-17 14:08pm    
Try commenting your code and see the difference.
CodingIsDreamy 2-Nov-17 14:21pm    
my code is above
Mrunal Sonawane 2-Nov-17 14:22pm    
xD
CodingIsDreamy 2-Nov-17 14:20pm    
i dont understand what you mean?
Mrunal Sonawane 2-Nov-17 14:23pm    
He is saying that put your code in comment...
'This is a comment... Lol

The FSW doesn't sit around waiting for an event from NTFS. There are no such events. So how does the FSW get changes to the file system?

Polling.

Basically, the FSW goes to every directory and subdirectory it's told to watch and grabs the list of files for them. It calls ReadDirectoryChangesW function (Windows)[^] on each directory, building a database of what ReadDirectoryChangesW function (Windows)[^] changes have happened since the last time each directory was hit by the function call. The FSW doesn't sit around waiting for an event from NTFS. There are no such events. So how does the FSW get these changes?

Polling. Basically, the FSW goes to every directory and subdirectory it's told to watch and grabs the list of files for them. Basically, it calls ReadDirectoryChangesW function (Windows)[^] on each directory. The more directories and subdirectories, the longer each polling cycle takes. Once all the directories are processed on one pass, the cycle starts all over again at the top of the directory tree it's told to watch. This is what is taking up so much CPU time.

As it goes through each folder, it starts generating the events your code sees for the changes it finds.

Since you're telling the FSW to watch thousands of folders, it's taking a ton of CPU time polling all of them to get the list of files that changed.

Basically, you're using the FSW for a task it wasn't designed to handle.
 
Share this answer
 
v2
Comments
CodingIsDreamy 2-Nov-17 16:22pm    
thanks man
Not sure but try adding a thread sleep in the loop... For like 500ms
Try that and tell me if it works
 
Share this answer
 
Comments
CodingIsDreamy 2-Nov-17 14:28pm    
where should i add it? Example?
Mrunal Sonawane 2-Nov-17 14:34pm    
For Each hashByte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Thread.Sleep(500)
Next
CodingIsDreamy 2-Nov-17 14:38pm    
well dude it took it down a little bit but not much of an effect from what i saw :-)
Mrunal Sonawane 2-Nov-17 14:39pm    
Hmm...
Mrunal Sonawane 2-Nov-17 14:40pm    
Thinking...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900