Jump to content
Sign in to follow this  
Moonsurfer

Reduce CPU Usage

Recommended Posts

Moonsurfer

Hi all,

I've been using AutoIT for a while now and I think it's awesome! Normally, I haven't had the need to open a topic because there is so much documentation. However, with this one, I'm kind of stumped.

I'm writing a script that would run in the background and make USB drives read only if it detects that its not encrypted. I'm not finished but I've gotten a good bit finished. 

What I'm having trouble with is the CPU time it uses. It sits at 25% usage. I'm sure it has to do with my endless While loop, but what can I do to not reduce the speed of the script but also let it sit idle?

Any suggestions would be greatly appreciated! :-)

#include <WindowsConstants.au3>

; Initialize variables
Dim $installdir = @WindowsDir & "\USB-Lock\"

; Check if OS is 32 or 64 bit.
; Make sure Install Directory exists. If not, then it's created.
If FileExists($installdir) = 0 Then DirCreate($installdir)
If @OSArch = "X86" Then
    FileInstall("\\user2k\users\carcenea\scripts\usb\access\devcon\32-bit\devcon.exe", $installdir & "devcon.exe", 1)
Else
    FileInstall("\\user2k\users\carcenea\scripts\usb\access\devcon\32-bit\devcon.exe", $installdir & "devcon.exe", 1)
EndIf

; Initilize WMI Options
$strComputer = "."
$objWMIServices = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$sink = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($sink,"SINK_")
$objWMIServices.ExecNotificationQueryAsync ($sink, "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " & "TargetInstance ISA 'Win32_LogicalDisk'")

; Endless loop so that script never ends...
While 1

Wend

Func SINK_OnObjectReady($objObject, $objAsyncContext)
    If $objObject.TargetInstance.DriveType = 2 Then
        Select
            ; Drive insertion event
            Case $objObject.Path_.Class()="__InstanceCreationEvent"
                $drive = $objObject.TargetInstance.DeviceId
                If FileExists($drive & "\truecrypt\") Then
                    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies", "WriteProtect", "REG_DWORD", "0")
                    RunWait($installdir & 'devcon.exe restart usbstor*', @WorkingDir, @SW_HIDE)
                Else
                    MsgBox(48, "Unencrypted USB Drive Detected", "An unencrypted USB drive has been detected. Your USB drive will be placed in READ-ONLY mode. If you want to have full access to it, please contact the Helpdesk at x26333.")
                EndIf
;~                 MsgBox(0, "Drive Inserted", "Drive " & $objObject.TargetInstance.DeviceId & " has been added." & @CR)
            ; Drive removal event
            Case $objObject.Path_.Class()="__InstanceDeletionEvent"
                RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies", "WriteProtect", "REG_DWORD", "1")
;~                 MsgBox(0, "Drive Inserted", "Drive " & $objObject.TargetInstance.DeviceId & " has been removed."& @CR)
        EndSelect
    EndIf
EndFunc

Share this post


Link to post
Share on other sites
Moonsurfer

Thx Jon. That worked marvalously... kind of ashamed that I didn't think of that... :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×