ThreadPriority Enumeration
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt die Terminplanungspriorität eines Threadan.
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
- Vererbung
- Attribute
Felder
| Name | Wert | Beschreibung |
|---|---|---|
| Lowest | 0 | Dies Thread kann nach Threads mit jeder anderen Priorität geplant werden. |
| BelowNormal | 1 | Die Thread Kann nach Threads mit |
| Normal | 2 | Die Thread Kann nach Threads mit |
| AboveNormal | 3 | Die Thread Kann nach Threads mit |
| Highest | 4 | Dies Thread kann vor Threads mit einer anderen Priorität geplant werden. |
Beispiele
Das folgende Codebeispiel zeigt das Ergebnis einer Änderung der Priorität eines Threads. Drei Threads werden erstellt, die Priorität eines Threads wird auf BelowNormal festgelegt, und die Priorität einer Zweiten wird auf AboveNormal festgelegt. Jeder Thread erhöht eine Variable in einer while Schleife und wird für eine festgelegte Zeit ausgeführt.
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
Hinweise
ThreadPriority definiert den Satz aller möglichen Werte für eine Threadpriorität. Threadprioritäten geben die relative Priorität eines Threads im Vergleich zu einem anderen an.
Jeder Thread hat eine zugewiesene Priorität. Threads, die innerhalb der Laufzeit erstellt wurden, werden zunächst der Normal Priorität zugewiesen, während Threads, die außerhalb der Laufzeit erstellt wurden, ihre vorherige Priorität beibehalten, wenn sie die Laufzeit eingeben. Sie können die Priorität eines Threads abrufen und festlegen, indem Sie auf seine Priority Eigenschaft zugreifen.
Die Ausführung von Threads wird basierend auf ihrer Priorität geplant. Der Planungsalgorithmus, der verwendet wird, um die Reihenfolge der Threadausführung zu bestimmen, variiert je nach Betriebssystem. Das Betriebssystem kann die Threadpriorität auch dynamisch anpassen, da der Fokus der Benutzeroberfläche zwischen dem Vordergrund und dem Hintergrund verschoben wird.
Die Priorität eines Threads wirkt sich nicht auf den Zustand des Threads aus. der Status des Threads muss sein Running , bevor das Betriebssystem ihn planen kann.