Tuesday 21 March 2017

Terminate, immediately, processes using WMI

There is something wrong with my computer, specifically the disk drive and virus checking.  When I take an update the virus scanner kicks in and my computer is rendered dead slow.  The slowness also occurs at other times.  I have learnt to manage this situation.  I have extra drives to spread the load with data files.  I also have installed a copy of Chrome on same extra drives and this helps.

Chrome is the best browser IMHO.  However, it does spawn multiple processes.  When the slowness kicks in I can see the disk queue via the Performance Monitor (accessed via Task Manager) shoot up to 50 or so.  When this happens killing processes via Task Manager is slow.  I think Task Manager sends a message to the process's message queue saying "please quit".   But I need to take back control of my computer far quicker.

It seems that WMI and the Win32_Process objects we can terminate immediately all the chrome.exes.  Here is some code.  Caution, use with care.


Option Explicit

'**********************************************************************************
'* Terminate, immediately
'**********************************************************************************
Function QuitChrome()

    Dim objWMIService As Object
    Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
    Dim colItems As Object
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process" & _
               " WHERE Name = 'chrome.exe'", , 48)
    Dim objItem As Object
    For Each objItem In colItems
        objItem.Terminate
    Next

End Function




Some feedback for some readers have pointed out a nice command line equivalent to kill Chrome.exe


taskkill /f /im chrome.exe


Thanks for that. This is an Excel coding blog but thanks anyway.

No comments:

Post a Comment