Wednesday, January 20, 2021

Changing Execution Priority on Demand

Recently, a friend asked me if it was possible to change the execution priority for some tasks on the fly. The issue was that when a user on a terminal server runs a report in Stonefield Query, which can be computing intensive as it retrieves and massages the data required for the report, other users may experience slower response. I did some research and it turned out to be easy to do, thanks to Rick Strahl's wwDotNetBridge.

The following code changes the priority for the running application to BelowNormal (see the documentation for the System.Diagnostics.ProcessPriorityClass enum for the different priority levels):

do wwDotNetBridge
loBridge = GetwwDotNetBridge()
loProcess = loBridge.InvokeStaticMethod('System.Diagnostics.Process', 'GetCurrentProcess')
loProcess.PriorityClass = loBridge.GetEnumValue('System.Diagnostics.ProcessPriorityClass.BelowNormal')

(This assumes you have wwDotNetBridge.prg included in your project and wwDotNetBridge.dll and ClrHost.dll in the current folder or VFP path.)

Use this code just before some process you want to run at a lower priority. Use similar code but a different value, such as Normal, to set the priority back again after the process is done.

As I have said many times, there's almost nothing we can't do in VFP with wwDotNetBridge.


Unknown said...

This is really awesome for two reasons:
1) elegantly addresses the performance issue when running the report
2) shows how easy it is to do things and windows via dotnetbridge

Anonymous said...

there is also

DECLARE INTEGER GetCurrentProcess IN Win32api
SetPriorityClass(GetCurrentProcess(),0x00000020) && normal
SetPriorityClass(GetCurrentProcess(),0x00004000) && below normal
SetPriorityClass(GetCurrentProcess(),0x00008000) && above normal

if you need to avoid dotnetbridge for some reason.