How to call async method with some priority?

I need to call several methods asynchronously with different priorities.

My first idea was to use ThreadPool and change Thread priority as:

static void Run() { ThreadPool.QueueUserWorkItem(new WaitCallback(SomeMethod)); } static void SomeMethod(object o) { Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; // is this ok? // do some work here } 

Does it work or what do you recommend?

0
source share
4 answers

According to http://msdn.microsoft.com/en-us/library/0ka9477y.aspx , this will not work if you target 2.0, it refers to some differences in 3.5, but in particular, priority is not mentioned:

When not to use thread pool threads

There are several scenarios in which it is advisable to create and manage your own streams instead of stream stream streams:

  • You need a front end.

  • You need the thread to have a certain priority.

  • You have tasks that cause the thread to block for extended periods of time. A thread pool has a maximum number of threads, so a large number of blocked threads of a thread pool can prevent tasks from starting.

  • You need to put streams in a single-threaded apartment. All ThreadPool threads are in a multi-threaded apartment.

  • You must have a persistent identifier associated with the thread, or a thread allocation for the task.

You will most likely have to come up with your own implementation and handle the creation of threads directly.

Question: What are you trying to achieve, you have a set of tasks that need to be processed, and you want the priority tasks to be performed first and the lower ones later; or do you really need themes with different priorities?

+4
source

This is definitely a terrible idea.

Generally speaking, setting a thread or process priority is a bad idea because it is not deterministic and you can starve from other threads / processes in action. In addition, you can really increase the priority of threads / processes with lower priority due to starvation.

In addition, thread pool threads are for reuse, and by changing the priority of the thread, you change the wait for the task that will receive the thread for use after starting your program.

If you have two options. If you only need to prioritize your tasks and not mind if other elements that are not related to your tasks are executed before you, then you can use the thread pool with a consumer-producer template, with some wrapper code that will take the highest priority from your turn.

If you want threads to perform only your tasks, you need to create your own thread pool (using the System.Thread class), and then do the same using the shell code so that the task runs based on priority.

New classes in the System.Threading namespace in .NET 4.0 (not yet released) will handle creating a separate thread pool for you.

+1
source

I think you should be clean, you should also restore the priority of the thread when you exit the method! (use try ... finally or something similar)

0
source

here is an old but very detailed thread pool implementation that supports different priorities.

0
source

Source: https://habr.com/ru/post/1447762/


All Articles