Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Thread slow down system

Thread slow down system

Scheduled Pinned Locked Moved Visual Basic
performance
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gagan 20
    wrote on last edited by
    #1

    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
    Next

        Do 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

    P D 2 Replies Last reply
    0
    • G Gagan 20

      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
      Next

          Do 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

      P Offline
      P Offline
      Paulo Zemek
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • G Gagan 20

        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
        Next

            Do 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

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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 the MonitorList? 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...

        G 1 Reply Last reply
        0
        • P Paulo Zemek

          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.

          G Offline
          G Offline
          Gagan 20
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            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 the MonitorList? 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...

            G Offline
            G Offline
            Gagan 20
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • G Gagan 20

              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

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups