Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
you know where in task manager you can set the priority of a thread, so it gets max cpu time, well is there any way to do that in c# code to make my app top priority??
Posted

There are several ways to approach it.

Here's an example of 2 ways.

1 uses the process name to set the thread priority of all of the threads in all of the processes with the matching name.

2 uses the process id to set the thread priority of all of the threads found in a specific process id.

 

static void SetPriority(string processName, System.Threading.ThreadPriority priority)
{
if(processName == null)
	throw new ArgumentNullException("processName");
System.Diagnostics.Process[] proccesses = System.Diagnostics.Process.GetProcessesByName(processName);
foreach(System.Diagnostics.Process process in proccesses)
		foreach(System.Threading.Thread thread in process.Threads)
			thread.Priority = priority;
}

static void SetPriority(int processId, System.Threading.ThreadPriority priority)
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processId);
foreach(System.Threading.Thread thread in process.Threads)
	thread.Priority = priority;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...