Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the time slices devoted to different running applications

Status
Not open for further replies.

gazzaboy

Programmer
Jul 24, 2002
1
0
0
AU
Hi,

I have a few VB applications running at the one time. I need to have one run reliably to collect data, and not be delayed for seconds while other applications take up CPU time eg starting a print job, or doing lots of calculations.
How can I do this?

Thanks,

Garry McKay
 
A simple (quick and dirty) solution is to add some DoEvent statements. This will force the current thread to pause and give other threads the oportunity to go on with their tasks. But be careful with this!

What I would recommend is implementing free threading. But this is not so simple. You would have to make a single threaded activeX that functions as a state controller. This state controller, manages all the worker-threads.

If you are planning to use free threading, let me know if you want more info! BTW free threading is a lot easier in VB.NET.

Greetz,
Jan If this response was usefull to you, please mark it with a Star!
 
First off, I'd read the following article:

Pay particular attention to the somewhat understated warnings that mucking about with thread priorities can seriously screw up your machine...

Having taken that on board, the following is a simple example of how you might change priority (example just gets current priorities, but is should be reasonably obvious that you can replace the GetPriority calls with SetPriority instead). And before proceeding, I'll just repeat: playing with thread and process priorities can seriously damage the health of Windows
[tt]
Option Explicit
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long

Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

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

Private Const MAXLONG = &H7FFFFFFF

' Six CONST declarations for use in setting process priorities

' Follwing two consts are only valid for W2000 and above
Private Const BELOW_NORMAL_PRIORITY_CLASS = &H4000
Private Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000

' Valid for all Windows versions from W95 onwards
' Indicates a normal process with no special scheduling needs.
Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const IDLE_PRIORITY_CLASS = &H40
Private Const HIGH_PRIORITY_CLASS = &H80
Private Const REALTIME_PRIORITY_CLASS = &H100

' CONST declarations for setting thread priorities
Private Const THREAD_BASE_PRIORITY_IDLE = -15
Private Const THREAD_BASE_PRIORITY_LOWRT = 15
Private Const THREAD_BASE_PRIORITY_MAX = 2
Private Const THREAD_BASE_PRIORITY_MIN = -2
Private Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
Private Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
Private Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
Private Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
Private Const THREAD_PRIORITY_ERROR_RETURN = (MAXLONG)
Private Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
Private Const THREAD_PRIORITY_NORMAL = 0
Private Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long


Private Sub Command1_Click()
Dim hProcess As Long
Dim hThread As Long

hProcess = GetCurrentProcessId
hThread = GetCurrentThread ' Pseudo handle with maximum permissions

' Ensure we have necessary permissions on the process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_SET_INFORMATION, 0, hProcess)

MsgBox "Process priority: " & GetPriorityClass(hProcess)
MsgBox "Thread priority: " & GetThreadPriority(hThread) 'THREAD_PRIORITY_ERROR_RETURN means we had problem, probably incorrect access rights

CloseHandle hProcess
CloseHandle hThread ' Don't really need to do this with a pseudohandle
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top