ThreadPriority 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定 Thread的排程優先順序。
public enum class ThreadPriority
public enum ThreadPriority
[System.Serializable]
public enum ThreadPriority
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ThreadPriority
type ThreadPriority =
[<System.Serializable>]
type ThreadPriority =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadPriority =
Public Enum ThreadPriority
- 繼承
- 屬性
欄位
| 名稱 | 值 | Description |
|---|---|---|
| Lowest | 0 | 這些可以 Thread 排程在執行緒之後,並有其他優先權。 |
| BelowNormal | 1 | 這些可以 Thread 排程在有 |
| Normal | 2 | 這些可以 Thread 排程在有 |
| AboveNormal | 3 | 這些可以 Thread 排程在有 |
| Highest | 4 | 它們 Thread 可以排程在執行緒之前,且有其他優先順序。 |
範例
以下程式碼範例展示了更改執行緒優先順序的結果。 會建立三個執行緒,其中一個執行緒的優先權設為 BelowNormal,另一個執行緒的優先權設為 AboveNormal。 每個執行緒在迴圈中增加一個變數 while ,並執行固定時間。
using System;
using System.Threading;
using Timers = System.Timers;
class Test
{
static void Main()
{
PriorityTest priorityTest = new PriorityTest();
Thread thread1 = new Thread(priorityTest.ThreadMethod);
thread1.Name = "ThreadOne";
Thread thread2 = new Thread(priorityTest.ThreadMethod);
thread2.Name = "ThreadTwo";
thread2.Priority = ThreadPriority.BelowNormal;
Thread thread3 = new Thread(priorityTest.ThreadMethod);
thread3.Name = "ThreadThree";
thread3.Priority = ThreadPriority.AboveNormal;
thread1.Start();
thread2.Start();
thread3.Start();
// Allow counting for 10 seconds.
Thread.Sleep(10000);
priorityTest.LoopSwitch = false;
}
}
class PriorityTest
{
static volatile bool loopSwitch;
[ThreadStatic] static long threadCount = 0;
public PriorityTest()
{
loopSwitch = true;
}
public bool LoopSwitch
{
set{ loopSwitch = value; }
}
public void ThreadMethod()
{
while(loopSwitch)
{
threadCount++;
}
Console.WriteLine("{0,-11} with {1,11} priority " +
"has a count = {2,13}", Thread.CurrentThread.Name,
Thread.CurrentThread.Priority.ToString(),
threadCount.ToString("N0"));
}
}
// The example displays output like the following:
// ThreadOne with Normal priority has a count = 755,897,581
// ThreadThree with AboveNormal priority has a count = 778,099,094
// ThreadTwo with BelowNormal priority has a count = 7,840,984
Imports System.Threading
Imports Timers = System.Timers
Public Module Example
Dim t As Timers.Timer
Private priorityTest As New PriorityTest()
Public Sub Main()
Dim thread1 As New Thread(AddressOf priorityTest.ThreadMethod)
thread1.Name = "ThreadOne"
Dim thread2 As New Thread(AddressOf priorityTest.ThreadMethod)
thread2.Name = "ThreadTwo"
thread2.Priority = ThreadPriority.BelowNormal
Dim thread3 As New Thread(AddressOf priorityTest.ThreadMethod)
thread3.Name = "ThreadThree"
thread3.Priority = ThreadPriority.AboveNormal
thread1.Start()
thread2.Start()
thread3.Start()
' Allow threads to execute for about 10 seconds.
t = New Timers.Timer()
t.AutoReset = False
t.Interval = 10000
AddHandler t.Elapsed, AddressOf Elapsed
t.Start()
End Sub
Private Sub Elapsed(sender As Object, e As Timers.ElapsedEventArgs)
priorityTest.LoopSwitch = False
End Sub
End Module
Public Class PriorityTest
Private Shared loopSwitchValue As Boolean
<ThreadStatic> Shared threadCount As Long
Sub New()
loopSwitchValue = True
End Sub
WriteOnly Property LoopSwitch As Boolean
Set
loopSwitchValue = Value
End Set
End Property
Sub ThreadMethod()
Do While True
threadCount += 1
If Not loopSwitchValue Then Exit Do
Loop
Console.WriteLine("{0,-11} with {1,11} priority " &
"has a count = {2,13}", Thread.CurrentThread.Name,
Thread.CurrentThread.Priority.ToString(),
threadCount.ToString("N0"))
End Sub
End Class
' The example displays the following output:
' ThreadOne with Normal priority has a count = 755,897,581
' ThreadThree with AboveNormal priority has a count = 778,099,094
' ThreadTwo with BelowNormal priority has a count = 7,840,984
備註
ThreadPriority 定義了執行緒優先權所有可能值的集合。 執行緒優先順序指定了各執行緒相對於另一執行緒的相對優先權。
每個執行緒都有指定的優先順序。 執行時內建立的執行緒初始被分配 Normal 優先權,而執行時外建立的執行緒進入執行時會保留先前的優先權。 你可以透過存取執行 Priority 緒的屬性來取得並設定執行緒的優先順序。
執行緒會根據它們的優先權排定執行。 用來決定執行緒執行順序的排程演算法會因作業系統而異。 作業系統也能動態調整執行緒優先順序,因為使用者介面的焦點在前景與背景間移動。
執行緒的優先順序不會影響執行緒的狀態;執行緒的狀態必須在作業系統排程前達到 Running 。