【问题标题】:Cross-thread operation not valid: Control 'chart1' accessed from a thread other than the thread it was created on跨线程操作无效:控件“chart1”从创建它的线程以外的线程访问
【发布时间】:2016-06-11 12:39:24
【问题描述】:

在某些情况下,我会尝试更新我的图表。我用另一种方法调用Plot 方法。我找到了this 解决方案,但它是针对文本框的,所以我无法在我的代码中实现它。我在互联网上搜索,但找不到图表的解决方案。

如何解决向图表添加点的问题?

private void Plot()
    {
            chart1.Series["test1"].Points.AddXY
                            (plotValues[0,1], plotValues[0,0]);
            chart1.Series["test1"].Points.AddXY
                           (plotValues[1, 1], plotValues[1, 0]);

    }

【问题讨论】:

  • 链接中的答案到底有什么问题?

标签: c# multithreading charts delegates invoke


【解决方案1】:
Dispatcher.Invoke(() => chart1.Series["test1"].Points.AddXY
                        (plotValues[0,1], plotValues[0,0]));

(或 InvokeAsync 以防您要运行是异步的/不需要等待其他线程上的操作完成)

【讨论】:

    猜你喜欢
    • 2011-01-15
    • 2016-05-21
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多