我再次遇到此错误:调用线程无法访问此对象,因为不同的线程拥有它。
这是我的代码:
public void InitiateSignalAnalysisPlot()
{
if (_nActiveChannel > 0) // timeDomainPlotter init
{
_dataX = new List<double[]>();
_dataY = new List<double[]>();
_dataX3 = new List<List<double[]>>();
_dataY3 = new List<List<double[]>>();
double[] dataXOneCh = new double[1];
double[] dataYOneCh = new double[1];
dataXOneCh[0] = 0;
dataYOneCh[0] = 0;
CirclePointMarker pm = new CirclePointMarker { Size = 5, Fill = Brushes.Transparent };
for (int i = 0; i < _nActiveChannel; i++)
{
if (_nActiveStatsOneChannel > 0)
{
for (int j = 0; j < _nActiveStatsOneChannel; j++)
{
_dataX.Add(dataXOneCh); // data x-y mapping init
_dataY.Add(dataYOneCh);
EnumerableDataSource<double> xOneCh = new EnumerableDataSource<double>(dataXOneCh);
EnumerableDataSource<double> yOneCh = new EnumerableDataSource<double>(dataYOneCh);
xOneCh.SetXMapping(xVal => xVal);
yOneCh.SetXMapping(yVal => yVal);
CompositeDataSource dsOneCh = new CompositeDataSource(xOneCh, yOneCh);
Action InitiatePlotter = delegate()
{
LineAndMarker<MarkerPointsGraph> lam = timeDomainPlotter.AddLineGraph(dsOneCh,
new Pen(pm.Fill, 2),
pm,
new PenDescription("C" + Convert.ToString(i) + "S" + Convert.ToString(j)));
};
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, InitiatePlotter);
}
_dataX3.Add(_dataX);
_dataY3.Add(_dataY);
}
}
timeDomainPlotter.FitToView();
}
else
{
return;
}
}
错误发生在"this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, InitiatePlotter);"
这是由主窗口线程调用的 WPF 窗口后面的一段代码。我很困惑我使用 dispatcher.invoke() 来取消这个多线程冲突,为什么我仍然收到这个错误?如果我将这段代码放入被调用窗口的构造函数中,它可以工作,但我只是不想那样做。我可以做些什么改变来避免这个错误?谢谢。
可能有帮助的更多信息:timeDomainPlotter 在 wpf 窗口 B 中,它在窗口 A 中启动 (WindowB _windowB = new WindowB(); );上面的代码 InitiateSignalAnalysisPlot() 也在窗口 B 中,但在窗口 A 中调用了 InitiateSignalAnalysisPlot(),类似于 _windowB,InitiateSignalAnalysisPlot();
如果我不 Dispatcher.Invoke() 它,也会有一个异常,即The calling thread must be STA, because many UI components require this.
发生在
LineAndMarker<MarkerPointsGraph> lam = timeDomainPlotter.AddLineGraph(dsOneCh,
new Pen(pm.Fill, 2),
pm,
new PenDescription("C" + Convert.ToString(i) + "S" + Convert.ToString(j)));