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>