this.dispatcher.invoke()对多线程不起作用吗? [英] this.dispatcher.invoke() does not work for multithreading?

查看:173
本文介绍了this.dispatcher.invoke()对多线程不起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次遇到此错误:调用线程无法访问该对象,因为其他线程拥有它.

I am having this error again: The calling thread cannot access this object because a different thread owns it.

这是我的代码:

 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()来使多线程冲突无效,为什么我仍然收到此错误?如果我将这段代码放在被调用的窗口的构造函数中,那么它可以工作,但是我只是不想这样做.我可以做些什么改变来避免出现此错误?谢谢.

The error occurs at "this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, InitiatePlotter);" This is a piece of code behind a WPF window called by the main window thread. I am confused that I have used dispatcher.invoke() to void this multithread conflicting, why I am still getting this error? If I put this piece of code int he constructor of the window being called, it works, but I just don't want to do that way. What change can I make to I avoid this error? Thanks.

更多信息可能会有所帮助: timeDomainPlotter在wpf窗口B中,该窗口在窗口A中被启动(WindowB _windowB = new WindowB(););上面的代码InitiateSignalAnalysisPlot()也在窗口B中,但是InitiateSignalAnalysisPlot()在窗口A中被调用,类似于_windowB,InitiateSignalAnalysisPlot();.

More information that might help : The timeDomainPlotter is in wpf window B, which is initiated (WindowB _windowB = new WindowB(); ) in window A; The code above, InitiateSignalAnalysisPlot(), is also in window B, but InitiateSignalAnalysisPlot() gets called in window A, something like _windowB,InitiateSignalAnalysisPlot();

如果我不使用Dispatcher.Invoke(),也会在

If I don't Dispatcher.Invoke() it, there will also be an exception, which is The calling thread must be STA, because many UI components require this., happening at

LineAndMarker<MarkerPointsGraph> lam = timeDomainPlotter.AddLineGraph(dsOneCh,
                                                                                       new Pen(pm.Fill, 2),
                                                                                       pm,
                                                                                       new PenDescription("C" + Convert.ToString(i) + "S" + Convert.ToString(j)));

推荐答案

您在哪里创建 timeDomainPlotter?

Where do you create timeDomainPlotter?

在不同线程上创建的控件具有不同的分派器.我的猜测是您遇到此错误是因为您是通过'this.Dispatcher'调用委托的,但是您在该委托中接触的控件是在不同的线程上创建的,因此具有不同的调度程序这个".

Controls created on different threads have different Dispatchers. My guess is that you're hitting this error because you're invoking your delegate via 'this.Dispatcher', but the control(s) you're touching within that delegate were created on a different thread and thus have a different dispatcher to 'this'.

如果它在不同的线程上,也许您可​​以为创建创建一个委托,并通过this.Dispatcher调用该委托,那么控件将在您稍后尝试访问它的同一线程上创建.

If it's on a different thread, maybe you could set up a delegate for the creation and call that delegate via this.Dispatcher, then the control will be created on the same thread that you're later trying to access it on.

这篇关于this.dispatcher.invoke()对多线程不起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆