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. |
|
Private Declare Function apiOpenProcess _
Lib "kernel32" Alias "OpenProcess" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) _
As Long
Private Declare Function apiSetPriorityClass _
Lib "kernel32" Alias "SetPriorityClass" _
(ByVal hProcess As Long, _
ByVal dwPriorityClass As Long) _
As Long
Private Declare Function apiGetPriorityClass _
Lib "kernel32" Alias "GetPriorityClass" _
(ByVal hProcess As Long) _
As Long
Private Declare Function apiGetWindowThreadProcessId _
Lib "user32" Alias "GetWindowThreadProcessId" _
(ByVal hWnd As Long, _
lpdwProcessId As Long) _
As Long
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
Public Const BELOW_NORMAL_PRIORITY_CLASS = &H4000
Public Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000
Public Const NORMAL_PRIORITY_CLASS = &H20
Public Const IDLE_PRIORITY_CLASS = &H40
Public Const HIGH_PRIORITY_CLASS = &H80
Public Const REALTIME_PRIORITY_CLASS = &H100
Function fChangeAccessPriority(lngPriority As Long) As Boolean
On Error GoTo ErrHandler
Dim lpProcessID As Long
Dim hProcess As Long
Dim lngRet As Long
Call apiGetWindowThreadProcessId(hWndAccessApp, lpProcessID)
hProcess = apiOpenProcess( _
PROCESS_QUERY_INFORMATION Or _
PROCESS_SET_INFORMATION, _
False, _
lpProcessID)
If Not hProcess = 0 Then
lngRet = apiGetPriorityClass(hProcess)
If Not lngRet = lngPriority Then
lngRet = apiSetPriorityClass(hProcess, lngPriority)
fChangeAccessPriority = Not (lngRet = 0)
Else
fChangeAccessPriority = True
End If
End If
Call apiCloseHandle(hProcess)
ExitHere:
Exit Function
ErrHandler:
fChangeAccessPriority = False
Resume ExitHere
End Function
|