【发布时间】:2013-11-18 16:57:48
【问题描述】:
在过去的 30 分钟里,我一直在破解这个问题,试图线程化一个简单的 foreach 循环,无论我做什么都会抛出一些错误(第一次不使用线程框架,所以我最可能会犯一些愚蠢的语法错误)
遗憾的是我不能使用Parallel.For,因为必须保持 .net 3.5 或以下版本...有人可以告诉我正确的方法,这样我就可以回到不想尖叫的状态!!
须藤代码
void SomeMethod
{
foreach(Touch Input in Inputlist){
Thread thread = new Thread(new ThreadStart(this.FilterInput(Input)));
thread.Start();
}
}
void FilterInput(Input UnFilteredInput){
....
}
编辑:MonoDevelop 出现以下错误
表达式表示一个值,其中类型或方法组是 预计
最佳重载方法匹配 System.Threading.Thread.Thread(System.Threading.ThreadStart) 有 一些无效的参数,
参数 #1 无法将对象表达式转换为类型 System.Threading.ThreadStart
【问题讨论】:
-
尝试在循环顶部添加
Touch copy = Input;行并将线程开始更改为Thread thread = new Thread(new ThreadStart(this.FilterInput(copy))); -
即使出现了同样的 3 个错误 ::
(Expression denotes a value, where a type or method group was expected), (The best overloaded method match for System.Threading.Thread.Thread (System.Threading.ThreadStart) has some invalid arguments), (3: Argument #1 cannot convert object expression to type System.Threading.ThreadStart) -
抛开转换错误,你可能会创建太多线程,除非你在循环中放置信号量或其他东西。
-
它是一个封闭的更新系统输入不超过 8 个滴答声,每秒不超过 50 个滴答声 所以我希望这不会成为问题
-
好点 Andrew :: 这就是我在凌晨 1:00 编码时发生的情况 :) .... ThreadPool 是 100% 的路要走!
标签: c# multithreading for-loop foreach c#-3.0