Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to display priority of Thread
In C#, thread priority determines how the operating system schedules threads for execution. The Priority property of the Thread class allows you to both view and set the priority of a thread. Thread priority is represented by the ThreadPriority enumeration.
Syntax
To get the current thread and display its priority −
Thread thread = Thread.CurrentThread; ThreadPriority priority = thread.Priority;
To set a thread's priority −
thread.Priority = ThreadPriority.High;
ThreadPriority Enumeration Values
| Priority Level | Description |
|---|---|
| Lowest | The thread has the lowest priority |
| BelowNormal | The thread has below normal priority |
| Normal | The thread has normal priority (default) |
| AboveNormal | The thread has above normal priority |
| Highest | The thread has the highest priority |
Displaying Current Thread Priority
Example
using System;
using System.Threading;
class MyClass {
static void Main(string[] args) {
Thread thread = Thread.CurrentThread;
thread.Name = "My Thread";
Console.WriteLine("Thread Name: {0}", thread.Name);
Console.WriteLine("Thread Priority: {0}", thread.Priority);
Console.WriteLine("Thread State: {0}", thread.ThreadState);
}
}
The output of the above code is −
Thread Name: My Thread Thread Priority: Normal Thread State: Running
Setting and Displaying Different Thread Priorities
Example
using System;
using System.Threading;
class ThreadPriorityDemo {
static void Main(string[] args) {
Thread currentThread = Thread.CurrentThread;
currentThread.Name = "Main Thread";
Console.WriteLine("Default Priority: {0}", currentThread.Priority);
// Set different priorities and display
currentThread.Priority = ThreadPriority.Highest;
Console.WriteLine("After setting Highest: {0}", currentThread.Priority);
currentThread.Priority = ThreadPriority.Lowest;
Console.WriteLine("After setting Lowest: {0}", currentThread.Priority);
currentThread.Priority = ThreadPriority.AboveNormal;
Console.WriteLine("After setting AboveNormal: {0}", currentThread.Priority);
// Reset to normal
currentThread.Priority = ThreadPriority.Normal;
Console.WriteLine("Reset to Normal: {0}", currentThread.Priority);
}
}
The output of the above code is −
Default Priority: Normal After setting Highest: Highest After setting Lowest: Lowest After setting AboveNormal: AboveNormal Reset to Normal: Normal
Creating Custom Threads with Priorities
Example
using System;
using System.Threading;
class CustomThreadDemo {
static void ThreadMethod(object priority) {
Thread current = Thread.CurrentThread;
Console.WriteLine("Thread {0} - Priority: {1}", current.Name, current.Priority);
}
static void Main(string[] args) {
Thread thread1 = new Thread(ThreadMethod);
thread1.Name = "Thread1";
thread1.Priority = ThreadPriority.High;
Thread thread2 = new Thread(ThreadMethod);
thread2.Name = "Thread2";
thread2.Priority = ThreadPriority.Low;
Console.WriteLine("Starting threads with different priorities:");
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("Main thread priority: {0}", Thread.CurrentThread.Priority);
}
}
The output of the above code is −
Starting threads with different priorities: Thread Thread1 - Priority: High Thread Thread2 - Priority: Low Main thread priority: Normal
Conclusion
Thread priority in C# provides a way to influence thread scheduling by the operating system. Use the Priority property to get or set thread priority levels ranging from Lowest to Highest. Remember that priority is a suggestion to the OS scheduler, not an absolute guarantee of execution order.
