Thread slow down system
-
Hi all... I am creating a project to monitor files of USB disk each time when it is inserted. When a USB disk is inserted, a thread starts and checks all executable files in usb disk. when user run any executable in usb disk then thread immediately stops its execution. The code is working properly but it slow down the system. The code is below :
Private Sub StartMonitoring()
Dim MonitorList As New List(Of String)For Each drive As DriveInfo In System.IO.DriveInfo.GetDrives If drive.DriveType = DriveType.Removable And drive.Name <> "A:\\" Then DriveName = drive End If Next
For Each file As String In My.Computer.FileSystem.GetFiles(DriveName.Name,FileIO.SearchOption.SearchAllSubDirectories)
If IO.Path.GetExtension(File) = ".exe" Then
MonitorList.Add(File)
End If
NextDo While Not memThd Is Nothing 'memThd is thread which is started to monitor. Try For Each prcName As String In MonitorList For Each prc As Process In Process.GetProcesses If IO.Path.GetFileName(prcName) = prc.ProcessName & ".exe" Then prc.Kill() catch ex as exception msgbox(ex.message) End Try Loop End Sub
The code is working perfectly but it slow down the system. Suggest me some way to maintain system speed while running this thread or some other way to write the above code. Thanks Gagan
-
Hi all... I am creating a project to monitor files of USB disk each time when it is inserted. When a USB disk is inserted, a thread starts and checks all executable files in usb disk. when user run any executable in usb disk then thread immediately stops its execution. The code is working properly but it slow down the system. The code is below :
Private Sub StartMonitoring()
Dim MonitorList As New List(Of String)For Each drive As DriveInfo In System.IO.DriveInfo.GetDrives If drive.DriveType = DriveType.Removable And drive.Name <> "A:\\" Then DriveName = drive End If Next
For Each file As String In My.Computer.FileSystem.GetFiles(DriveName.Name,FileIO.SearchOption.SearchAllSubDirectories)
If IO.Path.GetExtension(File) = ".exe" Then
MonitorList.Add(File)
End If
NextDo While Not memThd Is Nothing 'memThd is thread which is started to monitor. Try For Each prcName As String In MonitorList For Each prc As Process In Process.GetProcesses If IO.Path.GetFileName(prcName) = prc.ProcessName & ".exe" Then prc.Kill() catch ex as exception msgbox(ex.message) End Try Loop End Sub
The code is working perfectly but it slow down the system. Suggest me some way to maintain system speed while running this thread or some other way to write the above code. Thanks Gagan
I must say that if drive is A your code will not work, as you don't set DriveName but use the variable. Also... what's the purpose of this? Kill any process started from the pen-drive? Are you trying to avoid viruses this way? But, about the fact it is slow, any non-stopping code makes the computer slow. You can try to set the thread priority to Idle... this could help.
-
Hi all... I am creating a project to monitor files of USB disk each time when it is inserted. When a USB disk is inserted, a thread starts and checks all executable files in usb disk. when user run any executable in usb disk then thread immediately stops its execution. The code is working properly but it slow down the system. The code is below :
Private Sub StartMonitoring()
Dim MonitorList As New List(Of String)For Each drive As DriveInfo In System.IO.DriveInfo.GetDrives If drive.DriveType = DriveType.Removable And drive.Name <> "A:\\" Then DriveName = drive End If Next
For Each file As String In My.Computer.FileSystem.GetFiles(DriveName.Name,FileIO.SearchOption.SearchAllSubDirectories)
If IO.Path.GetExtension(File) = ".exe" Then
MonitorList.Add(File)
End If
NextDo While Not memThd Is Nothing 'memThd is thread which is started to monitor. Try For Each prcName As String In MonitorList For Each prc As Process In Process.GetProcesses If IO.Path.GetFileName(prcName) = prc.ProcessName & ".exe" Then prc.Kill() catch ex as exception msgbox(ex.message) End Try Loop End Sub
The code is working perfectly but it slow down the system. Suggest me some way to maintain system speed while running this thread or some other way to write the above code. Thanks Gagan
It slows down the entire system because your code is horribly inefficient. Did you notice that you're calling
Process.GetProcesses
(kind of expensive) each and every time you look at a new item in theMonitorList
? You're also consuming 100% of the CPU since your loops doesn't yield at all. You also have the problem of killing off a legit process running off the hard drive with the same name as one stored on the stick. Killing off a process rather abruptly opens the system up for data corruption and system instability if the app doesn't get the chance to close gracefully. Your have two "fixes" for the performance problem. First, switch your loops so your only getting the Process list once per pass. Second, put in a Thread.Sleep (with an appropriate value) to put this thread to sleep for a second or two before it goes for another pass. Look, you really can't stop someone from running applications off of a USB stick like this. There is a group policy that can stop this without you constantly killing the CPU to do it. It's also quite easy to kill off your process and launch an app anyway.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
I must say that if drive is A your code will not work, as you don't set DriveName but use the variable. Also... what's the purpose of this? Kill any process started from the pen-drive? Are you trying to avoid viruses this way? But, about the fact it is slow, any non-stopping code makes the computer slow. You can try to set the thread priority to Idle... this could help.
Paulo Zemek wrote:
I must say that if drive is A your code will not work
I have already checked if drv.DriveName<>"A:" then starting the thread.
Paulo Zemek wrote:
You can try to set the thread priority to Idle...
I set the prority of threat to 0 (i.e. lowest priority) but it has no effect. Suggest me some other ways. Thanks. Gagan
-
It slows down the entire system because your code is horribly inefficient. Did you notice that you're calling
Process.GetProcesses
(kind of expensive) each and every time you look at a new item in theMonitorList
? You're also consuming 100% of the CPU since your loops doesn't yield at all. You also have the problem of killing off a legit process running off the hard drive with the same name as one stored on the stick. Killing off a process rather abruptly opens the system up for data corruption and system instability if the app doesn't get the chance to close gracefully. Your have two "fixes" for the performance problem. First, switch your loops so your only getting the Process list once per pass. Second, put in a Thread.Sleep (with an appropriate value) to put this thread to sleep for a second or two before it goes for another pass. Look, you really can't stop someone from running applications off of a USB stick like this. There is a group policy that can stop this without you constantly killing the CPU to do it. It's also quite easy to kill off your process and launch an app anyway.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Dave Kreskowiak wrote:
Your have two "fixes" for the performance problem. First, switch your loops so your only getting the Process list once per pass.
I created a list of all running processes and stored it in ProcessList. Now I am comparing the executable file stored in MonitorList with process name stored in Process List.
Dave Kreskowiak wrote:
Second, put in a Thread.Sleep (with an appropriate value) to put this thread to sleep for a second or two before it goes for another pass.
I put this thread to sleep for 1 second as memThd.sleep(1000) after one iteration. But it does not speeds up my system. Suggest me some other ways. Thanks. Gagan
-
Dave Kreskowiak wrote:
Your have two "fixes" for the performance problem. First, switch your loops so your only getting the Process list once per pass.
I created a list of all running processes and stored it in ProcessList. Now I am comparing the executable file stored in MonitorList with process name stored in Process List.
Dave Kreskowiak wrote:
Second, put in a Thread.Sleep (with an appropriate value) to put this thread to sleep for a second or two before it goes for another pass.
I put this thread to sleep for 1 second as memThd.sleep(1000) after one iteration. But it does not speeds up my system. Suggest me some other ways. Thanks. Gagan
I have no idea what you're app is doing to tie up the CPU so badly. If you rewrote the code, properly, the way I suggested, this wouldn't be a problem anymore. Of course, without seeing the modified code, it's impossible for me to tell you what's going wrong.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...