c#线程池类ThreadPool的QueueUserWorkItem方法不能调用有参数的方法吗...

发布网友 发布时间:2024-10-24 13:57

我来回答

3个回答

热心网友 时间:2分钟前

可以的啊

ThreadPool.QueueUserWorkItem(
                           new WaitCallback(obj =>
                           {
                                 sTime = DateTime.Now;
                                 foreach (Node node in this.ImportNodes.Where(p => p.TNode.NodeCode.Substring(2, 1) + p.TNode.NodeCode.Substring(7, 1) == "11" || p.TNode.NodeCode.Substring(2, 1) + p.TNode.NodeCode.Substring(7, 1) == "81").ToList())
                                 {
                                       ThreadCount++;
                                       UserTime = DateTime.Now - sTime;
                                       new Thread(new ParameterizedThreadStart(CalculateTPath)).Start(node);
                                       while (true)
                                       {
                                             CpuUser = Math.Round(CpuWatch.NextValue(), 2);
                                             UserTime = DateTime.Now - sTime;
                                             if (ThreadCount < MaxThreadCount)
                                             {
                                                   break;
                                             }
                                             Thread.Sleep(50);
                                       }
                                       //CpuUser = Math.Round(CpuWatch.NextValue(), 2);
                                       //UserTime = DateTime.Now - sTime;
                                       Thread.Sleep(50);
                                 }
                           })
                     );

这个是我以前写的,你看中间有多少个带参的方法啊

热心网友 时间:6分钟前

是的 这个委托默认是无参的

热心网友 时间:8分钟前

可以的,它有两有重载方式。调用时一种带参数,一种不带参数

而且你看回调函数的定义就是带参数的。如下



QueueUserWorkItem(WaitCallback) 
QueueUserWorkItem(WaitCallback, Object)
[ComVisibleAttribute(true)]
public delegate void WaitCallback( Object state )
// http://msdn.microsoft.com/zh-cn/library/vstudio/system.threading.threadpool.queueuserworkitem(v=vs.100).aspx
// http://msdn.microsoft.com/zh-cn/library/vstudio/system.threading.waitcallback(v=vs.100).aspx

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com