An example from andreycha throttling pattern:

 public async Task CheckServers() { var servers = new List<string>(10000) { ... }; const int ConcurrencyLevel = 100; // запускаем первые 100 запросов var tasks = servers.Take(ConcurrencyLevel).Select(GetVersion).ToList(); int nextIndex = ConcurrencyLevel; while (tasks.Count > 0) { // дожидаемся завершения любого запроса var completedTask = await Task.WhenAny(tasks); // удаляем его из списка tasks.Remove(completedTask); // добавляем новый запрос, если таковые остались if (nextIndex < servers.Count) { tasks.Add(GetVersion(servers[nextIndex++])); } string rfbVersion = await completedTask; // работаем с версией } } 

How in such implementation it is nice to set the TaskCreationOptions.LongRunning parameter TaskCreationOptions.LongRunning tasks? Is this even possible without using Task.Factory.StartNew ?

  • No Not. It is not necessary. - Athari
  • @Athari, yes well, you need this, because it increases the software performance by ~ 20-30% if you use Task.Factory.StartNew. If this was not to be confused, I would not ask. - Alexis

1 answer 1

See it. Let's divide Task 'and other asynchronous functions into those that are limited by the processor (that is, they take a fixed thread for a long time), and the rest.

For the first functions, TaskCreationOptions.LongRunning makes sense, because we don’t want to block the stream from the thread pool for a long time. But such functions are usually provided in the form of synchronous functions that we run through Task.Run . So you can, if desired, run through Task.Factory.StartNew .

For the remaining asynchronous functions, there is no concept of a “stream in which the function runs,” and most of the time Task does not run anywhere , just waiting for the await end. TaskCreationOptions.LongRunning meaningless for such functions, and it is difficult to set it (if at all). But in this case, this option is not needed.

  • I am working on increasing the speed, this is roughly the continuation of this question: ru.stackoverflow.com/questions/503721/… - Alexis
  • In my case, by remaking the pattern on Task.Factory.StartNew - it was possible to increase productivity, since protocol cryptography gives the load mainly on the processor, thanks for the clarification. I just wanted to know if there was a way to make the code more beautiful and simple. - Alexis
  • one
    @Alexis: There seems to be no other way, at least I don’t know. - VladD