According to MSDN: If the BeginInvoke method is called, the common language runtime (CLR) will queue the request and return immediately to the caller. The target method will be called on a thread from the thread pool.
不对,再更正下 if ( someEvent!= null) { Delegate[] dels=someEvent.GetInvocationList(); foreach (Delegate del in dels) { TheadPool.public static bool QueueUserWorkItem (delegate(){ someHander tim = (someHander)del;
{
Delegate[] dels=someEvent.GetInvocationList();
foreach (Delegate del in dels)
{
TheadPool.public static bool QueueUserWorkItem (delegate(){ someHander tim = (someHander)del;
tim.BeginInvoke(dtTime, null, null); }); //线程池 }
}
If the BeginInvoke method is called, the common language runtime (CLR) will
queue the request and return immediately to the caller. The target method
will be called on a thread from the thread pool.
if ( someEvent!= null)
{
Delegate[] dels=someEvent.GetInvocationList();
foreach (Delegate del in dels)
{
TheadPool.public static bool QueueUserWorkItem (delegate(){ someHander tim = (someHander)del;
tim.Invoke(dtTime, null, null); }); //因为是线程池了,所以就不用再Begin了,直接Invoke }
}
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic61277.aspx
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic61277.aspx
你可以在invoke函数中打印出CurrentThread.Id来获得threadid,看一下一共有多少个线程在执行。
ThreadPool.SetMaxThreads(5, 5);谢谢大家的关注了,一会儿结贴。尤其谢谢bclz_vs的提醒!
const int threadCount=5;bool flag = dels.Length>50;int[] threadArray=new int[0];
if(flag)
{
threadArray=new int[dels.Length/threadCount -1];
for(int i=1,len=threadArray.Length+1;i<len;i++)
{
threadArray[i-1]=threadCount*i+1;
}
}-----------------------------------------------------------------------
if(!flag)
{
foreach (Delegate del in dels)
{
someHander tim = (someHander)del;
tim.BeginInvoke(dtTime, null, null);
}}
else
{
//定义线程并实例化线程
int index=1;
foreach (Delegate del in dels)
{
//do sth
if(threadArray.Contains(index))
{
//让线程实例开启
//重新实例化线程实例
}
}
}
http://topic.csdn.net/u/20100914/11/06b8ca43-c391-4755-a692-990f8a5cd675.html?seed=623418642&r=68409230#r_68409230
81楼