I am trying to change the priority of a thread but I can't get it to work. I have made a button which toggles the priority between low and high and when I am checking this in the joblist the priority is changed. But the CPU usage isn't changed. I wonder if this is just because I don't use the full CPU power or how this can be.
I am not asking if it is a good idea. I am asking how to do it.
Here is how I change the priority. This is the code behind class:
private Thread tr;
public MainWindow()
{
InitializeComponent();
tr = new Thread(new ThreadStart(infiniteLoop));
tr.Start();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Process.GetCurrentProcess().PriorityClass == ProcessPriorityClass.High)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
tr.Priority = ThreadPriority.Lowest;
description.Text = "Idle";
}
else
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
tr.Priority = ThreadPriority.Highest;
description.Text = "High";
}
}
private void infiniteLoop()
{
while (true)
{
}
}
It doesn't work how I think you expect it to - i.e. that a process with low priority is somehow throttled to x% CPU time.
A single-threaded process with any priority can consume 100% of CPU time on one core.
When you have two processes, one with higher priority than the other, that would both consume 100% CPU time on their own, executing simultaneously, the one with higher priority will get all of the CPU time (assuming a single core) and the one with lower priority will not progress at all.
The priority of a process is simply used to determine the order in which the scheduler will "hand out" CPU time slices to processes.