我有一个执行长时间I/O的外部库。我希望创建一个多线程应用程序,它将使用线程池来限制同时执行的线程数
和
我希望将处理这些外部调用的线程添加为完成端口线程(I/O线程),而不是工作线程(以便计算绑定线程的限制)是完整的。
我有一个代码示例,它省略了外部库,但显示了我已经尝试过的内容。
有人知道怎么做吗?或者是可能的。谢谢你
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);
}
}
}