BLOG.CSHARPHELPER.COM: Run Threads with different priorities in C#
Run Threads with different priorities in C#
This example runs three different threads at different priorities. Each thread executes the following Counter class's Run method.
class Counter
{
// This counter's number.
public string Name;
// Initializing constructor.
public Counter(string name)
{
Name = name;
}
// Count off 10 half second intervals in the Output window.
public void Run()
{
for (int i = 1; i <= 10; i++)
{
// Display the next message.
Console.WriteLine(Name + " " + i);
// See when we should display the next message.
DateTime next_time = DateTime.Now.AddSeconds(0.5);
// Waste half a second. We don't sleep or call
// DoEvents so we don't give up control of the CPU.
while (DateTime.Now < next_time)
{
// Wait a bit.
}
}
}
}
The Counter class's constructor simply records its name so it can later display it in the Console window. The Run method loops from 1 to 10 displaying the Counter's name and the loop count in thue Console window.
The main program uses the MakeThread method described shortly to create threads. The following code shows how the program creates its threads.
This code starts by declaring variables to hold the three possible threads. The btnHigh button's Click event handler calls the following MakeThread method passing it the variable that should hold a reference to the thread object, the thread's name, and its desired priority. The event handlers that create Normal and Low priority threads work similarly. The final event handler creates threads with all three priorities, creating the Low priority thread first and the High priority thread last.
// Make a thread with the indicated priority.
private void MakeThread(ref Thread thread_variable, string thread_name, ThreadPriority thread_priority)
{
// If the thread is already running, do nothing.
if ((thread_variable != null) && (thread_variable.IsAlive)) return;
// Make the thread.
Counter new_counter = new Counter(thread_name);
thread_variable = new Thread(new_counter.Run);
thread_variable.Priority = thread_priority;
thread_variable.IsBackground = true;
thread_variable.Name = thread_name;
// Start the thread.
thread_variable.Start();
}
The MakeThread method examines the thread variable. If there is already a thread with this priority and that thread is still running, the method exits.
Otherwise the method creates a new Counter object and a Thread object to execute its Run method. It sets the thread's priority, indicates that it should run in the background, sets its name, and starts it.
As the operating system sees fit, it swaps among the different Threads (and whatever else is running on the system). When you click the Start All Threads button, you will see that the High priority Thread gets the most CPU time, the Normal priority Thread gets the second most, and the Low priority Thread gets the least. In the trial shown in the picture, the High and Normal priority threads received roughly the same amount of CPU time and the Low priority thread received much less so it only got halfway through its count before the other threads finished even though it started first.
Comments