View Full Version : Solved: Running VBA as Low Priority
markh1182
06-11-2007, 08:04 AM
Hi, I have code that creates a table based on parameters passed through a stored procedure. 
 
If it has to get back a lot of data, Word can be running at up to 100% of CPU usage in Task Manager.
 
Is it therefore possible to be able to have the code running as lower priority, and therefore freeing up the CPU so users can carry on working?
 
Thanks, Mark
Ebrow
06-12-2007, 05:24 AM
Hi.
 
You could insert a DoEvents in to your code (where there is an intensive loop).  However this can be risky depending on what your code is doing.  But worth a try.
markh1182
06-12-2007, 06:21 AM
Hi, my code is looping through a set of records produced for grabbing data from a stored procedure, and inserting them into a table that the macro creates as it loops through each field of the data it returns.
 
I haven't used DoEvents before so am unsure as what to do.
Ebrow
06-12-2007, 12:52 PM
Just add DoEvents as a line of code.  Insert it in the code were the it loops through each record and places the data.
Give it a shot.  It might make no difference, or could cause other problems.
I would be interested in the "real" answer to your question.
Ebrow
06-12-2007, 12:54 PM
Example
Do
    'do this
    'do that
    'do this aswell
    'might as well do this too
    DoEvents
Loop Until somethinghappens
markh1182
06-20-2007, 03:23 AM
unfortunately that does not make a difference.
 
Does anyone else have any ideas? I did find a knowledgebase article for terminating a process so not sure if that could be amended to change priority.
markh1182
06-20-2007, 04:14 AM
Following on from my previous entry I have amended the code found in kbase for terminating a process and the following gives you the number of the corresponding priority, but not sure how to then update to set the priority to low:
 
8 = Normal
4 = Low for the priority values
 
Sub ProcessPriority()
    Dim strProcessName As String 'The variable to hold the process
    Dim objWMIcimv2 As Object
    Dim objProcess As Object
    Dim objList As Object
    Dim intError As Integer
     
    strProcessName = "notepad.exe" 'Process, change notepad.exe to the process you want
     
    Set objWMIcimv2 = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace
     
    Set objList = objWMIcimv2.ExecQuery _
    ("select * from win32_process where name='" & strProcessName & "'") 'Find the process
     
    If objList.Count = 0 Then 'If 0 then process isn't running
        MsgBox strProcessName & " is NOT running." & vbCr & vbCr _
        & "Exiting procedure.", vbCritical, "Unable to continue"
        Set objWMIcimv2 = Nothing
        Set objList = Nothing
        Set objProcess = Nothing
        Exit Sub
    Else
             
            For Each objProcess In objList
                 
                MsgBox objProcess.Priority 'Priority of process
                
            Next
             
            Set objWMIcimv2 = Nothing
            Set objList = Nothing
            Set objProcess = Nothing
            Exit Sub
             
    End If
     
End Sub
markh1182
06-21-2007, 03:57 AM
Solved this one. Needed to add a CONST in for BELOW_NORMAL = 16384 and then add in a line in my FOR section objProcess.SetPriority (BELOW_NORMAL).
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.