【发布时间】:2012-05-24 08:39:01
【问题描述】:
可能重复:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
WPF access GUI from other thread
美好的一天, 我写课
public class Metric1
{
public event MetricUnitEventHandler OnUnitRead;
public void ReiseEventOnUnitRead(string MetricUnitKey)
{
if (OnUnitRead!=null)
OnUnitRead(this,new MetricUnitEventArgs(MetricUnitKey));
}
.....
}
Metric1 m1 = new Metric1();
m1.OnUnitRead += new MetricUnitEventHandler(m1_OnUnitRead);
void m1_OnUnitRead(object sender, MetricUnitEventArgs e)
{
MetricUnits.Add(((Metric1)sender));
lstMetricUnit.ItemsSource = null;
lstMetricUnit.ItemsSource = MetricUnits;
}
然后我启动新线程,每分钟调用 m1 的 ReiseEventOnUnitRead 方法。
在行中lstMetricUnit.ItemsSource = null;抛出异常 - “调用线程无法访问此对象,因为另一个线程拥有它。” 为什么?
【问题讨论】:
-
这已被多次询问和回答。这是List
标签: c# .net wpf multithreading events