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.