3

I have an action,

var act = new Action(() =>
{
   while (true)
   {
     //Some Codes!
   }
 });
 act.BeginInvoke(null, null);

How can I increase the priority of the thread that runs this action? I know how can I do it in simple thread.

Thread.CurrentThread.Priority = ThreadPriority.Lowest;

But how about an action's priority?

Sam
  • 7,252
  • 16
  • 46
  • 65
mefmef
  • 665
  • 2
  • 11
  • 23
  • It's unclear what you want to do. Apart from that, don't change the priority of a thread manually, it means little. If you have performance problems then see if you can improve the code rather than playing with thread priorities since the behavior is undefined and can vary – Polity Mar 17 '12 at 06:07
  • There's no point, the thread doesn't do anything. – Hans Passant Mar 17 '12 at 06:11
  • i rewrite my question in order to make it more clear , as my program have 2 thread and i want to make ones higher! – mefmef Mar 17 '12 at 06:12

1 Answers1

4

BeginInvoke will queue your task to the ThreadPool. You have no control over the dispatching of the standard .NET ThreadPool. You can only control the thread once your code actually executes.

WARNING: Changing priority of a ThreadPool Thread is considered dangerous. Further Reading: .NET: Why not change the priority of a ThreadPool (or Task) thread?

If you can explain what you're trying to achieve, perhaps you can get a better solution?

Community
  • 1
  • 1
  • i try to write some codes for a humanoid robot witch have 2 different Action one for motion controlling and one for vision process. some time because of lacking resources it falls , i think that if i change the priority of motion thread it may fix the problem! – mefmef Mar 17 '12 at 06:16
  • 1
    The ThreadPool is intended for code that is okay to execute in the background, when resources are available. For things which require guarantee of execution, you should spin up a dedicated thread for running those. –  Mar 17 '12 at 06:20