求问关于C#中多线程问题
try
{
Thread t = new Thread(() =>
{
for (int tmpi = 0; tmpi < dsFangjixue.Tables[0].Rows.Count; tmpi++)
{
this.Dispatcher.Invoke(new Action(() =>
{
FormulaXuhao = tmp_xuhao[tmpi];
this.DataGrid1.Dispatcher.BeginInvoke(new myDelegate(init));
}));
}
});
t.Start();
}
catch (System.Exception ex)
{
MessageBox.Show(this, ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
上面是我的代码,其中FormulaXuhao是我定义的全局整数型数值,其可取值1、2、3、4、5、6,然后使用这个多线程方法进行运行。
进行断点调试,for循环中内容有时执行有时不执行,最后结果导致FormulaXuhao只能1取到1到6中的2个值或3个值等等,即我本身要生成六个线程可只生成了2个或3个线程,求大神解答,困扰了一天了。。。