在一个新开始的线程内跨线程访问单个控件这样写:checkbox1.Dispatcher.BeginInvoke(new Action(() => checkbox1.IsChecked = true));没有问题;如果UI的stackPanel内有20个checkBox,想使用for或foreach执行上面的代码,全部赋值为true,怎么做?
下面的写法编译没有问题,但会报错"跨线程不能访问"(sp1为StackPanel控件,这个代码在同线程里没有问题):
((CheckBox)sp1.Children[i]).Dispatcher.BeginInvoke(new Action(() => ((CheckBox)sp1.Children[i]).IsChecked = true));
http://blog.csdn.net/u011555996/article/details/72295145
http://blog.csdn.net/u011555996/article/details/72295145
this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate ()
{
foreach(var item in sp1.Children)
{
var chb = item as CheckBox;
chb.IsChecked = true;
}
}));