TaskCreationOptions.LongRunning

ずっと気になっていたので試してみた。

まず普通の `Task.Factory.StartNew`。

```csharp:Program.normal.cs
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BackgroundTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int workerThreads, completionPortThreads;
            
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);
            
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);

            Console.WriteLine("--");

            Task.Factory.StartNew(() =>
            {
                ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                while (true) { }
            
            });

            Console.Read();
        }
    }
}
```
![GetAvailableThreads の戻り値が 1 減っている](https://photos-4.dropbox.com/t/2/AAAYyRkhssV0XuC0jWVzZc9HO0-vuux0Qjzcv1JeYluguA/12/7605502/png/32x32/3/1496437200/0/2/TaskNormal.png/ENaB0wUYr7ACIAcoBw/y7VYrWOuGro3tsX3n16W8lk5s4T31Zqd68cx2_aq1F4?dl=0&size=2048x1536&size_mode=3)

次に `TaskCreationOptions.LongRunning` を指定。

```csharp:Program.long.cs
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BackgroundTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int workerThreads, completionPortThreads;
            
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);
            
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);

            Console.WriteLine("--");

            Task.Factory.StartNew(() =>
            {
                ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                while (true) { }
            
            }, TaskCreationOptions.LongRunning);

            Console.Read();
        }
    }
}
```
![GetAvailableThreads の戻り値に変化なし](https://photos-4.dropbox.com/t/2/AAC_LFd-USfdzhVG3QH2pTgn8VvpE_0Sje2Tdhxa3wDdBg/12/7605502/png/32x32/3/1496437200/0/2/TaskLongRunning.png/ENaB0wUYr7ACIAcoBw/IuAiYDum6rUjJUYqQK4gitPvEfuSt39DAq-CTGAbgRc?dl=0&size=32x32&size_mode=5)

ということで、`LongRunning` を指定すれば ThreadPool の利用可能ワーカースレッド数を減らさずに済むようだ。