To snowring (飘飘然) :您可以尝试使用.NET提供的Threadpool机制。.NET为每个进程提供一个唯一的System Threadpool,默认情况下可以存放最多25个线程。通过ThreadPool.QueueUserWorkItem()方法,可以创建一个工人线程并将其送入Threadpool,余下的调度与执行都由Threadpool负责完成。构造该工人线程的函数必须是void(object)类型的,开发者可以将另一函数的delegate作为参数传递进去以实现回调函数。.NET的Threadpool的默认容量是25个。通过修改一个名为CorSetMaxThreads的常量可以改变线程池的容量。在microsoft.public.dotnet.framework.sdk讨论组中有一位Microsoft MVP给出了一段通过编程修改线程池容量的例子代码,我将其转载如下:
/************ 转载开始 *****************/SUBJECT EMAIL DATE
Re: Increasing number of threads in ThreadPool [email protected] 3/29/2002 Sure you can, but you have to create a Interop assembly by hand (tlbimp is of no help here) based on the definitions in mscoree.h.-------------------------------------
using System;
using System.Runtime.InteropServices;/*
class DECLSPEC_UUID("CB2F6723-AB3A-11d2-9C40-00C04FA30A3E")CorRuntimeHost;*/[
Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E"),
ComImport
]
class CorRuntimeHost
{
}
/*MIDL_INTERFACE("84680D3A-B2C1-46e8-ACC2-DBC0A359159A")ICorThreadpool : public IUnknown*/[
Guid("84680D3A-B2C1-46e8-ACC2-DBC0A359159A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
interface ICorThreadPool
{
void slot0(); // v-table dummy slots, as you are only interested in the max threadpool settings
void slot1();
void slot2();
void slot3();
void slot3();
void slot4();
void slot5();
void slot6();
void CorSetMaxThreads( uint MaxWorkerThreads, uint MaxIOCompletionThreads );
void CorGetMaxThreads( out uint MaxWorkerThreads, out uint MaxIOCompletionThreads );
void CorGetAvailableThreads( out uint AvailableWorkerThreads, out uint AvailableIOCompletionThreads );
}
------------------------------Compile this into a library and call it as:..... uint maxWorkerThreads;
uint availWorkerThreads;
uint maxIOThreads;
uint availIOThreads; ICorThreadPool clrTp clrTp = (ICorThreadPool)new CorRuntimeHost(); clrTp .CorGetMaxThreads(out maxWorkerThreads, out maxIOThreads);
clrTp .CorGetAvailableThreads(out availWorkerThreads, out availIOThreads); clrTp .CorSetMaxThreads(...
....Willy."Paul Ireland" <[email protected]> wrote in message news:u9I8e#l1BHA.2212@tkmsftngp05...
> Does anyone know how to increase the number of threads in the ThreadPool?
> The MSDN docs state:
>
> "The thread pool has a default limit of 25 threads per available processor,
> which could be changed using CorSetMaxThreads as defined in the mscoree.h
> file."
>
> Any ideas which component implements CorSetMaxThreads so I can call it?
>
> Being able to set the max number of threads in the thread pool is important
> because classes such as HttpWebRequest use thread pools.
>
> Paul Ireland
>
>
>
>/************ 转载结束 *****************/- 微软全球技术中心 acptvc本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
/************ 转载开始 *****************/SUBJECT EMAIL DATE
Re: Increasing number of threads in ThreadPool [email protected] 3/29/2002 Sure you can, but you have to create a Interop assembly by hand (tlbimp is of no help here) based on the definitions in mscoree.h.-------------------------------------
using System;
using System.Runtime.InteropServices;/*
class DECLSPEC_UUID("CB2F6723-AB3A-11d2-9C40-00C04FA30A3E")CorRuntimeHost;*/[
Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E"),
ComImport
]
class CorRuntimeHost
{
}
/*MIDL_INTERFACE("84680D3A-B2C1-46e8-ACC2-DBC0A359159A")ICorThreadpool : public IUnknown*/[
Guid("84680D3A-B2C1-46e8-ACC2-DBC0A359159A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
interface ICorThreadPool
{
void slot0(); // v-table dummy slots, as you are only interested in the max threadpool settings
void slot1();
void slot2();
void slot3();
void slot3();
void slot4();
void slot5();
void slot6();
void CorSetMaxThreads( uint MaxWorkerThreads, uint MaxIOCompletionThreads );
void CorGetMaxThreads( out uint MaxWorkerThreads, out uint MaxIOCompletionThreads );
void CorGetAvailableThreads( out uint AvailableWorkerThreads, out uint AvailableIOCompletionThreads );
}
------------------------------Compile this into a library and call it as:..... uint maxWorkerThreads;
uint availWorkerThreads;
uint maxIOThreads;
uint availIOThreads; ICorThreadPool clrTp clrTp = (ICorThreadPool)new CorRuntimeHost(); clrTp .CorGetMaxThreads(out maxWorkerThreads, out maxIOThreads);
clrTp .CorGetAvailableThreads(out availWorkerThreads, out availIOThreads); clrTp .CorSetMaxThreads(...
....Willy."Paul Ireland" <[email protected]> wrote in message news:u9I8e#l1BHA.2212@tkmsftngp05...
> Does anyone know how to increase the number of threads in the ThreadPool?
> The MSDN docs state:
>
> "The thread pool has a default limit of 25 threads per available processor,
> which could be changed using CorSetMaxThreads as defined in the mscoree.h
> file."
>
> Any ideas which component implements CorSetMaxThreads so I can call it?
>
> Being able to set the max number of threads in the thread pool is important
> because classes such as HttpWebRequest use thread pools.
>
> Paul Ireland
>
>
>
>/************ 转载结束 *****************/- 微软全球技术中心 acptvc本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
解决方案 »
- 真心请教版主一个问题:.net程序员的未来之路。
- 翻页速度优化(asp 与 .net)问题?
- 怎样用ADO来打开一个DBF库(使用C#)
- 在Hashtable中该怎么做才可以逐项更改每一个数据?
- Listbox送分!
- winform 绘图如何遍历显示万年历
- 一个下午没搞出来的问题,请各位帮忙
- 问一下,怎么把excel文件读到datset里面
- newtonsoft.json将json转换为对象,自定义日期该如何设置?
- 如何点击窗口关闭后,弹出询问对话框,如果选择否,窗口不退出???
- UTF8Encoding的GetBytes得到汉字的编码为什么是3个字节,最后一个字节是0xaa
- 如何限制IE能够访问的站点?
To snowring(飘飘然):使用.NET Threadpool的例子以及程序在MSDN以及各类第三方网站都有大量介绍和下载,但出于您的方便考虑,还是提供一段简单的例子代码给您。下面这段代码使用了Threadpool来管理多线程,他的功能是:当用户按下button2的时候开始以每秒十次的频率计数,并把计数置显示在textBox1中;当用户按下button1或者计数指超过1000时停止计数。
/***** 代码开始 ******/ private void button1_Click(object sender, System.EventArgs e)
{
this.powerOn=false;
} private void button2_Click(object sender, System.EventArgs e)
{
this.powerOn=true;
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.Countdown),1000);
} private void Countdown(object Max)
{
if( ! (Max is int) )
{
return;
} this.textBox1.Text="";
int i=0;
while(this.powerOn && i<(int)Max)
{
i++;
this.textBox1.Text=i+"";
System.Threading.Thread.Sleep(100);
}
}/***** 代码结束 ******/
希望以上例子能对您有帮助,最后还是要感谢你关心和使用微软的产品和技术。- 微软全球技术中心 acptvc本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
{
this.powerOn=false;
} private void button2_Click(object sender, System.EventArgs e)
{
this.powerOn=true;
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.Countdown),1000);
} private void Countdown(object Max)
{
if( ! (Max is int) )
{
return;
} this.textBox1.Text="";
int i=0;
while(this.powerOn && i<(int)Max)
{
i++;
this.textBox1.Text=i+"";
System.Threading.Thread.Sleep(100);
}
}/***** 代码结束 ******/希望以上例子能对您有帮助,最后还是要感谢你关心和使用微软的产品和技术。- 微软全球技术中心 acptvc本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
{
this.powerOn=false;
}private void button2_Click_1(object sender, System.EventArgs e)
{
this.powerOn=true;
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.Countdown));
}private void Countdown(object none)
{
this.textBox1.Text="";
int i=0;
while(this.powerOn)
{
i++;
this.textBox1.Text=i+"";
System.Threading.Thread.Sleep(100);
}
}在这个例子里面,当用户按下button2的时候开始以每秒十次的频率计数,并把计数置显示在textBox1中;当用户按下button1时停止计数。希望这个例子能够对您理解threadpool有所帮助。
- 微软全球技术中心 VC技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
{
this.powerOn=false;
}private void button2_Click_1(object sender, System.EventArgs e)
{
this.powerOn=true;
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.Countdown));
}private void Countdown(object none)
{
this.textBox1.Text="";
int i=0;
while(this.powerOn)
{
i++;
this.textBox1.Text=i+"";
System.Threading.Thread.Sleep(100);
}
}在这个例子里面,当用户按下button2的时候开始以每秒十次的频率计数,并把计数置显示在textBox1中;当用户按下button1时停止计数。希望这个例子能够对您理解threadpool有所帮助。
- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。