Thread.Priority Property
Gets or sets a value indicating the scheduling priority of a thread.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Threading.ThreadPriorityOne of the ThreadPriority values. The default value is Normal.
| Exception | Condition |
|---|---|
| ThreadStateException | The thread has reached a final state, such as Aborted. |
| ArgumentException | The value specified for a set operation is not a valid ThreadPriority value. |
The following code example shows the result of changing the priority of a thread. Two threads are created and the priority of one thread is set to BelowNormal. Both threads increment a variable in a while loop and run for a set time.
using System; using System.Threading; class Test { static void Main() { PriorityTest priorityTest = new PriorityTest(); ThreadStart startDelegate = new ThreadStart(priorityTest.ThreadMethod); Thread threadOne = new Thread(startDelegate); threadOne.Name = "ThreadOne"; Thread threadTwo = new Thread(startDelegate); threadTwo.Name = "ThreadTwo"; threadTwo.Priority = ThreadPriority.BelowNormal; threadOne.Start(); threadTwo.Start(); // Allow counting for 10 seconds. Thread.Sleep(10000); priorityTest.LoopSwitch = false; } } class PriorityTest { bool loopSwitch; public PriorityTest() { loopSwitch = true; } public bool LoopSwitch { set{ loopSwitch = value; } } public void ThreadMethod() { long threadCount = 0; while(loopSwitch) { threadCount++; } Console.WriteLine("{0} with {1,11} priority " + "has a count = {2,13}", Thread.CurrentThread.Name, Thread.CurrentThread.Priority.ToString(), threadCount.ToString("N0")); } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.