【问题标题】:Update oxyplot model from another thread从另一个线程更新 oxyplot 模型
【发布时间】:2018-04-23 00:56:21
【问题描述】:

这是我第一次使用 WPF,但遇到了问题。 我无法从另一个线程更新我的 oxyplot 模型。 我不能在另一个线程中画点,但是当我试图在另一个线程中这样做时,什么也没有发生。 现在我有了这个代码:

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        doComputingThread compute = new doComputingThread();
        Thread _MainThread = new Thread(new ThreadStart(compute.MainThread));
        _MainThread.Start();
    }

class doComputingThread{
   public doComputingThread()
    {
        DataPlot = new PlotModel();
        DataPlot.Series.Add(new LineSeries());
    }
    public void MainThread()
    {
        bool flag;

        _timer = new System.Timers.Timer();
        _timer.Interval = 10;
        _timer.Elapsed += (sender, e) => { GuiRefresher(true); };
        _timer.Enabled = true;

        Thread _ComputeThread = new Thread(new ThreadStart(ProducerThread));
        _ComputeThread.Start();
    }
    public void ProducerThread()
    {
        //populate queue

        int X = 0;
        int Y = 0;

        for (double t = 0; t < 2 * 3.14; t = t + 0.1)
        {
            X = (int)(Math.Cos(t) * 5000);
            Y = (int)(Math.Sin(t) * 5000);


            Coordinate.X = X;
            Coordinate.Y = Y;
            _queue.Enqueue(Coordinate);
        }
    public void GuiRefresher(object flag)
    {

        if (_queue.TryDequeue(out Coordinate))
        {
            //this part didn't refresh my oxyplot
            Dispatcher.CurrentDispatcher.Invoke(() =>
           {
               (DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(Coordinate.X, Coordinate.Y));
               DataPlot.InvalidatePlot(true);
           });

}

除了Dispatcher.CurrentDispatcher 时的部分外,所有工作都按预期工作。我不明白为什么我的情节没有刷新。

我有一些想法,在这种情况下,我不明白 WPF 的 UI 线程是什么线程,也许我应该在 doComputingThread 构造函数中启动我的线程。

Xaml:

<ui:WslMainWindow x:Class="fpga_control.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:oxy="http://oxyplot.org/wpf"
    xmlns:local="clr-namespace:fpga_control"
    xmlns:ui="clr-namespace:Keysight.Ccl.Wsl.UI;assembly=Keysight.Ccl.Wsl"
    xmlns:DynamicVectorImages="clr-namespace:Keysight.Ccl.Wsl.UI.Controls.DynamicVectorImages;assembly=Keysight.Ccl.Wsl" 
    Title="Example 1 (WPF)" Height="461.311" Width="621.393">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid >
    <oxy:PlotView Model="{Binding DataPlot}" Margin="10,10,152,0" Height="418" VerticalAlignment="Top"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="464,10,0,0" VerticalAlignment="Top" Width="137" Height="38" RenderTransformOrigin="0.303,1.929" Click="Button_Click"/>
    <TextBox x:Name="txb" HorizontalAlignment="Left" Height="23" Margin="468,53,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="133"/>
</Grid>

【问题讨论】:

    标签: c# wpf multithreading user-interface oxyplot


    【解决方案1】:

    您的代码似乎将相同的坐标添加到循环中的队列中。您不会在循环期间每次都创建一个新的 Coordinate 实例。

    您的_queue.TryDequeue(out Coordinate) 行也会对我产生语法错误,因为它试图将Coordinate 用作变量而不是类。

    如果没有Coordinate 的定义,我无法确认这些。

    另外,我看不到您实际将DataPlot 添加到表单或表单上的任何控件的位置。 无论如何,您似乎都没有显示任何内容.

    底线是您的示例代码不是一个最小的、完整的和可验证的示例。所以我只能猜测是什么问题。

    我会给你一个我认为会给你想要的东西的替代方案。

    首先,我为Coordinate 提供一个简单的定义:

    public class Coordinate
    {
        public int X;
        public int Y;
    }
    

    现在我将使用 Microsoft 的响应式框架来执行代码的所有线程、计算和分派 - 以下是我将如何编写 Button_Click 处理程序:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DataPlot = new PlotModel();
        DataPlot.Series.Add(new LineSeries());
    
        int steps = 60;
        IObservable<Coordinate> query =
            Observable
                .Generate(
                    0,
                    n => n < steps,
                    n => n + 1,
                    n => n * 2.0 * Math.PI / steps,
                    n => TimeSpan.FromMilliseconds(10.0))
                .Select(t => new Coordinate()
                {
                    X = (int)(Math.Cos(t) * 5000),
                    Y = (int)(Math.Sin(t) * 5000),
                });
    
        IDisposable subscription =
            query
                .ObserveOnDispatcher()
                .Subscribe(c =>
                {
                    (DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(c.X, c.Y));
                    DataPlot.InvalidatePlot(true);
                });
    }
    

    使用此代码,您将不再需要 doComputingThread 类。

    Observable.Generate 代码为您的原始代码生成的60 数据点生成t 值,但它每10.0 毫秒一次只生成一个值。这段代码实际上是一个计时器和t 步骤的生产者。

    .Selectt 值映射到Coordinate 值。

    subscriptionquery 的执行。第一步是使用.ObserveOnDispatcher() 调用将值编组回UI 线程,然后.Subscribe 获取每个值并在UI 线程上运行c =&gt; ... 委托。

    这应该很好地更新情节。

    如果您想提前停止绘图,只需致电subscription.Dispose(),它就会停止。

    您需要 NuGet "System.Reactive" 和 "System.Reactive.Windows.Threading" 来获取这些位。您还需要以下 using 语句来编译代码:

    using System.Reactive;
    using System.Reactive.Linq;
    

    别忘了 - 您仍然需要以某种方式将 DataPlot 添加到您的表单中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多