Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

API: Changing Access's Priority under NT/Win2000

Author(s)
Dev Ashish

Under Windows NT and Windows 2000, it's possible to view and change a process's priority by using the Windows Task Manager.  (Processes Tab | right click on any process and select Set Priority). A new process (including Access), as a default, uses Normal priority class which specifies that the process has no specific scheduling needs. However, as shown by NT's Task Manager, it's possible to temporarily assign a higher priority to a process so that more CPU slice is given by the system to that particular process.

The Win32 API allows us to use SetPriorityClass under Windows NT and Windows 2000 to change a process's priority.  We could, for example, use this API function to temporarily increase Access's priority so that a normally time consuming Query or process intensive code segments can run a bit faster.

Warnings:

Use HIGH_PRIORITY_CLASS with care. If a thread runs at the highest priority level for extended periods, other threads in the system will not get processor time. If several threads are set at high priority at the same time, the threads lose their effectiveness. The high-priority class should be reserved for threads that must respond to time-critical events. If your application performs one task that requires the high-priority class while the rest of its tasks are normal priority, use SetPriorityClass to raise the priority class of the application temporarily; then reduce it after the time-critical task has been completed. The important point is that a high-priority thread should execute for a brief time, and only when it has time-critical work to perform.
You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts system threads that manage mouse input, keyboard input, and background disk flushing. This class can be appropriate for applications that "talk" directly to hardware or that perform brief tasks that should have limited interruptions.

' ******** Code Start ********
'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
' function returns a handle to an existing process object
Private Declare Function apiOpenProcess _
    Lib "kernel32" Alias "OpenProcess" _
    (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) _
    As Long

' function sets the priority class for the specified process.
' This value together with the priority value of each thread
' of the process determines each thread's base priority level.
Private Declare Function apiSetPriorityClass _
    Lib "kernel32" Alias "SetPriorityClass" _
    (ByVal hProcess As Long, _
    ByVal dwPriorityClass As Long) _
    As Long

' function returns the priority class for the specified process.
Private Declare Function apiGetPriorityClass _
    Lib "kernel32" Alias "GetPriorityClass" _
    (ByVal hProcess As Long) _
    As Long

' retrieves the identifier of the thread that created the specified
' window and, optionally, the identifier of the process
' that created the window.
Private Declare Function apiGetWindowThreadProcessId _
    Lib "user32" Alias "GetWindowThreadProcessId" _
    (ByVal hWnd As Long, _
    lpdwProcessId As Long) _
    As Long

' closes an open object handle.
Private Declare Function apiCloseHandle _
    Lib "kernel32" Alias "CloseHandle" _
    (ByVal hObject As Long) _
    As Long


Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const PROCESS_SET_INFORMATION = &H200

' Win2000: Indicates a process that has priority above
' IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS.
Public Const BELOW_NORMAL_PRIORITY_CLASS = &H4000

' Win2000: Indicates a process that has priority above
' NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS.
Public Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000
' Win2000  end

' Indicates a normal process with no special scheduling needs.
Public Const NORMAL_PRIORITY_CLASS = &H20

' Indicates a process whose threads run only when the system is
' idle and are preempted by the threads of any process
' running in a higher priority class.
Public Const IDLE_PRIORITY_CLASS = &H40

' Indicates a process that performs time-critical tasks that
' must be executed immediately for it to run correctly.
' The threads of a high-priority class process preempt the threads
' of normal or idle priority class processes.
Public Const HIGH_PRIORITY_CLASS = &H80

' Indicates a process that has the highest possible priority.
' The threads of a real-time priority class process preempt the
' threads of all other processes, including operating system
' processes performing important tasks.
Public Const REALTIME_PRIORITY_CLASS = &H100

Function fChangeAccessPriority(lngPriority As Long) As Boolean
' -------------------------------
'         NT / WIN2000 ONLY
' -------------------------------
' Changes Access's priority under NT
' MAKE SURE YOU READ AND UNDERSTAND THE WARNINGS
'
On Error GoTo ErrHandler
Dim lpProcessID As Long
Dim hProcess As Long
Dim lngRet As Long

    ' Get ProcessID for Access
    Call apiGetWindowThreadProcessId(hWndAccessApp, lpProcessID)
    ' Get a handle to the current process
    hProcess = apiOpenProcess( _
                        PROCESS_QUERY_INFORMATION Or _
                        PROCESS_SET_INFORMATION, _
                        False, _
                        lpProcessID)
    If Not hProcess = 0 Then
        ' Get the current Priority
        lngRet = apiGetPriorityClass(hProcess)
        ' If the priority specified is not the same as
        ' what's returned by GetPriorityClass
        If Not lngRet = lngPriority Then
            ' then attempt to set the new priority for the process
            lngRet = apiSetPriorityClass(hProcess, lngPriority)
            fChangeAccessPriority = Not (lngRet = 0)
        Else
            ' Specified priority is the same as the current
            ' priority, so no need to call the API function
            fChangeAccessPriority = True
        End If
    End If
    ' close the handle
    Call apiCloseHandle(hProcess)
ExitHere:
    Exit Function
ErrHandler:
    fChangeAccessPriority = False
    Resume ExitHere
End Function
' ********* Code End ***********

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer