當我編寫下面的代碼時,爲什麼我會得到可用的線程號碼,如1022,1020。 因爲我使用線程池,所以我必須獲得最多25個線程。線程池中的活動線程號
我猜輸出線程數是系統上的可用線程。我需要在我的線程池中獲取可用的線程號,在win表單應用程序中。
private void Foo()
{
int intAvailableThreads, intAvailableIoAsynThreds;
// ask the number of avaialbe threads on the pool,
//we really only care about the first parameter.
ThreadPool.GetAvailableThreads(out intAvailableThreads,
out intAvailableIoAsynThreds);
// build a message to log
string strMessage =
String.Format(@"Is Thread Pool: {1},
Thread Id: {2} Free Threads {3}",
Thread.CurrentThread.IsThreadPoolThread.ToString(),
Thread.CurrentThread.GetHashCode(),
intAvailableThreads);
// check if the thread is on the thread pool.
Trace.WriteLine(strMessage);
// create a delay...
Thread.Sleep(30000);
return;
}
非常感謝..
(注:我的代碼http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx) 好文章!
毫無疑問,文章是優秀的,但它是在2006年寫的! – Azmeer 2017-01-10 02:03:47