The*_*ite 1 c# multithreading outofrangeexception

你可以清楚地看到args []的最高索引是2,但迭代器不知何故得到3.
编辑:评论的Thread.Sleep神奇地修复了问题.
这是由ifor循环之外的整个for循环声明引起的.由于无法保证Thread在该点执行,因此i可以在Thread执行之前更改值.你可以通过在for循环中声明一个局部变量来"解决"这个问题.
//for loop ..
var localIndex = i;
var temp = new Thread(() => PrintOut(args[localIndex], IsFilePath(args[localIndex])));
temp.Start();
//for loop ..
Run Code Online (Sandbox Code Playgroud)
编辑:另外,请你下次发一个代码片段,节省我再次写出代码:P
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |