Es ist mein erstes Mal, dass ich WPF und ich ein Problem haben. Ich kann mein Oxyplot-Modell nicht von einem anderen Thread aktualisieren. Ich kann in keinen anderen Thread zeichnen, aber wenn ich versuche, es in einem anderen zu tun passiert nichts. Jetzt habe ich diesen Code:Update Oxyplot-Modell aus einem anderen Thread
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);
});
}
Alle Arbeiten wie erwartet mit Ausnahme des Teils, wenn Dispatcher.CurrentDispatcher
. Ich habe nicht verstanden, warum meine Handlung nicht aktualisiert wurde.
Ich habe eine Idee, die ich nicht verstehe, was Thread ist UI-Thread in diesem Fall mit WPF und vielleicht sollte ich meine Threads in doComputingThread
Konstruktor initiieren.
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>