How to suspend the termination port (I / O binding function) to the CLR ThreadPool

I have an external library that performs lengthy I / O operations. I want to create a multi-threaded application that will use ThreadPool to limit the number of threads at the same time, and I want to add threads that send these external calls as a termination thread thread (I / O threads), rather than worker threads (so there is a limitation on thread related threads) is not damaged.

I have a sample code that omits an external library, but shows that I have already tried.

Does anyone know how to do this? Or is it even possible. Thanks you

using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ThreadPoolTest
{
    class MainApp
    {
        static void Main()
        {
            ThreadPool.SetMaxThreads(10, 10);
            ThreadPool.QueueUserWorkItem(DoWork); //doesn't work - a compute-bound thread 

            ThreadPool.SetMaxThreads(10, 10);

             //doesn't work - still a compute-bound thread 
            ((Action<object>)DoWork).BeginInvoke(null, Callback, null);

            Console.Read();
        }

        static void DoWork(object o)
        {
            ShowAvailableThreads();
            //call to external library - that does a long I/O operation
            Thread.Sleep(10);
        }

        static void Callback(IAsyncResult ar)
        {
            ShowAvailableThreads();
        }

        static void ShowAvailableThreads()
        {
            int workerThreads, completionPortThreads;

            ThreadPool.GetAvailableThreads(out workerThreads,
               out completionPortThreads);
            Console.WriteLine("WorkerThreads: {0}," +
               " CompletionPortThreads: {1}",
               workerThreads, completionPortThreads);
        }
    }
}
+3
1

-, .

QueueUserWorkItem " ". UnsafeQueueNativeOverlapped -, -, , ThreadPool BindHandle.

+4

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


All Articles