Advertisement
6_2008-2009 Windows API Call/ Explanation #195149

Changing priority

This code fragment demonstrates how you can get a program to change it's own priority. Sometimes it is necessary to change the priority of a process from the default. The example that prompted this code was a program I had to launch from a commercial scheduler always defaulted to idle priority. This caused the program to miss it's processing deadline. Increasing the priority to normal or high solved the problem.

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
Sub ChangePriority(dwPriorityClass As Long)
  Dim hProcess&
  Dim ret&, pid&
  pid = GetCurrentProcessId() ' get my proccess id
  ' get a handle to the process
  hProcess = OpenProcess(PROCESS_DUP_HANDLE, True, pid)
  If hProcess = 0 Then
    Err.Raise 2, "ChangePriority", "Unable to open the source process"
    Exit Sub
  End If
  ' change the priority
  ret = SetPriorityClass(hProcess, dwPriorityClass)
  ' Close the source process handle
  Call CloseHandle(hProcess)
  If ret = 0 Then
    Err.Raise 4, "ChangePriority", "Unable to close source handle"
    Exit Sub
  End If
End Sub
Private Sub Form_Load()
  Timer1.Interval = 2000
  Call Timer1_Timer
End Sub
Private Sub Timer1_Timer()
  Static Priority&
  If Priority = IDLE_PRIORITY_CLASS Then
   Priority = HIGH_PRIORITY_CLASS
   Label1.Caption = "HIGH priority"
  Else
   Label1.Caption = "IDLE priority"
   Priority = IDLE_PRIORITY_CLASS
  End If
  Call ChangePriority(Priority)
End Sub
Bình luận gốc (3)
Được khôi phục từ Wayback Machine