Thread・Classの優先順位設定と取得          <TOP>


GetCurrentThread カレントスレッドの擬似ハンドルを取得
GetCurrentProcess 現在のプロセスに対応する疑似ハンドルを取得
SetThreadPriority 指定したスレッドの優先順位を設定
SetPriorityClass 指定のプロセスのプライオリティクラスを設定
GetThreadPriority 指定したスレッドの優先順位を取得
GetPriorityClass 指定のプロセスのプライオリティクラスを取得
 

 

'================================================================
'= Thread・Classの優先順位設定と取得
'=    (SetPriorityClass2.bas)
'================================================================
#include "Windows.bi"

' カレントスレッドの擬似ハンドルを取得
Declare Function Api_GetCurrentThread& Lib "kernel32" Alias "GetCurrentThread" ()

' 現在のプロセスに対応する疑似ハンドルを取得
Declare Function Api_GetCurrentProcess& Lib "Kernel32" Alias "GetCurrentProcess" ()

' 指定したスレッドの優先順位を設定
Declare Function Api_SetThreadPriority& Lib "kernel32" Alias "SetThreadPriority" (ByVal hThread&, ByVal nPriority&)

' 指定のプロセスのプライオリティクラスを設定
Declare Function Api_SetPriorityClass& Lib "kernel32" Alias "SetPriorityClass" (ByVal hProcess&, ByVal dwPriorityClass&)

' 指定したスレッドの優先順位を取得
Declare Function Api_GetThreadPriority& Lib "kernel32" Alias "GetThreadPriority" (ByVal hThread&)

' 指定のプロセスのプライオリティクラスを取得
Declare Function Api_GetPriorityClass& Lib "kernel32" Alias "GetPriorityClass" (ByVal hProcess&)

#define THREAD_BASE_PRIORITY_IDLE -15   'デフォルト(Idle)
#define THREAD_BASE_PRIORITY_LOWRT 15   'デフォルト(Low)
#define THREAD_BASE_PRIORITY_MAX 2      'デフォルト(Max)
#define THREAD_BASE_PRIORITY_MIN -2     'デフォルト(Min)

#define THREAD_PRIORITY_LOWEST -2       'スレッド標準の相対優先順位値よりも2ポイント低い相対優先順位値(THREAD_BASE_PRIORITY_MIN)
#define THREAD_PRIORITY_HIGHEST 2       'スレッド標準の相対優先順位値よりも2ポイント高い相対優先順位値(THREAD_BASE_PRIORITY_MAX)
#define THREAD_PRIORITY_ABOVE_NORMAL 1  'スレッド標準の相対優先順位値よりも1ポイント高い相対優先順位値(THREAD_PRIORITY_HIGHEST-1)
#define THREAD_PRIORITY_BELOW_NORMAL -1 'スレッド標準の相対優先順位値よりも1ポイント低い相対優先順位値(THREAD_PRIORITY_LOWEST+1)
#define THREAD_PRIORITY_IDLE -15        '優先順位クラスが、IDLE_PRIORITY_CLASS、NORMAL_PRIORITY_CLASS、HIGH_PRIORITY_CLAS
#define THREAD_PRIORITY_NORMAL 0        'スレッド標準の相対優先順位値
#define THREAD_PRIORITY_TIME_CRITICAL 15'優先順位クラスが、IDLE_PRIORITY_CLASS、NORMAL_PRIORITY_CLASS、HIGH_PRIORITY_CLAS
#define THREAD_PRIORITY_ERROR_RETURN &H7FFFFFFF

#define IDLE_PRIORITY_CLASS &H40        'アイドルクラス(システムがアイドル状態のときにだけスレッドを実行するプロセスであることを指定)
#define NORMAL_PRIORITY_CLASS &H20      '通常クラス(一般的なプロセス)
#define HIGH_PRIORITY_CLASS &H80        '優先クラス(すぐに実行されなければならないタスクを実行するプロセスであることを指定)
#define REALTIME_PRIORITY_CLASS &H100   'リアルタイムクラス(最優先順位クラスを持つプロセスであることを指定)

Var Shared Text(3) As Object
Var shared Button1 As Object
Var Shared Group(1) As Object
Var Shared TRadio(6) As Object
Var Shared CRadio(3) As Object

For i = 0 To 6
    If i < 4 Then
        Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 12
        CRadio(i).Attach GetDlgItem("CRadio" & Trim$(Str$(i + 1))) :CRadio(i).SetFontSize 12
        If i < 2 Then
            Group(i).Attach GetDlgItem("Group" & Trim$(Str$(i + 1))) : Group(i).SetFontSize 12
        End If
    End If
    TRadio(i).Attach GetDlgItem("TRadio" & Trim$(Str$(i + 1))) : TRadio(i).SetFontSize 12
Next i
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

Var Shared hThread As Long
Var Shared hProcess As Long

'================================================================
'=
'================================================================
Declare Function optT bdecl () As Integer
Function optT()
    optT = Val(Mid$(GetDlgRadioSelect("TRadio1"), 7)) - 1
End Function

'================================================================
'=
'================================================================
Declare Function optC bdecl () As Integer
Function optC()
    optC = Val(Mid$(GetDlgRadioSelect("CRadio1"), 7)) - 1
End Function

'================================================================
'=
'================================================================
Declare Function GetPriority(strT As String, strC As String) As Integer
Function GetPriority(strT As String, strC As String) As Integer
    Var lThread As Long
    Var lClass As Long
    
    lThread = Api_GetThreadPriority(hThread)
    lClass = Api_GetPriorityClass(hProcess)
    
    If lThread = THREAD_PRIORITY_ERROR_RETURN Or lClass = 0 Then
        Exit Function
    End If
    
    GetPriority = True
    
    Select Case lClass
        Case IDLE_PRIORITY_CLASS
            strC = "IDLE_PRIORITY_CLASS"
        Case NORMAL_PRIORITY_CLASS
            strC = "NORMAL_PRIORITY_CLASS"
        Case HIGH_PRIORITY_CLASS
            strC = "HIGH_PRIORITY_CLASS"
        Case REALTIME_PRIORITY_CLASS
            strC = "REALTIME_PRIORITY_CLASS"
    End Select
    
    Select Case lThread
        Case THREAD_PRIORITY_LOWEST
            strT = "LOWEST"
        Case THREAD_PRIORITY_HIGHEST
            strT = "HIGHEST"
        Case THREAD_PRIORITY_ABOVE_NORMAL
            strT = "ABOVE_NORMAL"
        Case THREAD_PRIORITY_BELOW_NORMAL
            strT = "BELOW_NORMAL"
        Case THREAD_PRIORITY_IDLE
            strT = "IDLE"
        Case THREAD_PRIORITY_NORMAL
            strT = "NORMAL"
        Case THREAD_PRIORITY_TIME_CRITICAL
            strT = "TIME_CRITICAL"
    End Select
    
    strT = "THREAD_PRIORITY_" & strT
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Thread As String
    Var Class As String
    Var Ret As Long
    
    hThread = Api_GetCurrentThread()
    hProcess = Api_GetCurrentProcess()
    
    If GetPriority(Thread, Class) = True Then
        Text(0).SetWindowText Thread
        Text(1).SetWindowText Class
        
        Ret = Api_SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL)
        Ret = Api_SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS)
    Else
        A% = MessageBox("", "失敗しました!", 0, 2)
        End
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var lT As Long
    Var lC As Long
    Var Thread As String
    Var Class As String
    Var Ret As Long

    Select Case optT
        Case 0
            lT = THREAD_PRIORITY_LOWEST
        Case 1
            lT = THREAD_PRIORITY_HIGHEST
        Case 2
            lT = THREAD_PRIORITY_ABOVE_NORMAL
        Case 3
            lT = THREAD_PRIORITY_BELOW_NORMAL
        Case 4
            lT = THREAD_PRIORITY_IDLE
        Case 5
            lT = THREAD_PRIORITY_NORMAL
        Case 6
            lT = THREAD_PRIORITY_TIME_CRITICAL
    End Select
    
    Select Case optC
        Case 0
            lC = IDLE_PRIORITY_CLASS
        Case 1
            lC = NORMAL_PRIORITY_CLASS
        Case 2
            lC = HIGH_PRIORITY_CLASS
        Case 3
            lC = REALTIME_PRIORITY_CLASS
    End Select

    Ret = Api_SetThreadPriority(hThread, lT)
    Ret = Api_SetPriorityClass(hProcess, lC)
    
    If GetPriority(Thread, Class) = True Then
        Text(0).SetWindowText Thread
        Text(1).SetWindowText Class
    Else
        A% = MessageBox("", "失敗しました!", 0, 2)
        End
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End