WPF Interactive Data Display后台调用进程卡死窗口

2 篇文章 0 订阅

Interactive Data Display,这是微软官方的开源库,github网址为InteractiveDataDisplay.WPF,使用Interactive Data Display可以用VS自带的Nuget包管理工具安装,具体教程百度或者参照微软官方文档。
我根据微软官方文档,修改,并设计动态绘制折线图,实现折线图自动刷新功能。
Xmal

<d3:Chart Name="plotter" Margin="0,0,0,50" Background="Transparent">
	<d3:Chart.Title>
		<TextBlock HorizontalAlignment="Center" FontSize="18" Margin="0,5,0,5">Loss</TextBlock>
	</d3:Chart.Title>
	<d3:Chart.LegendContent>
		<d3:LegendItemsPanel>
			<d3:LegendItemsPanel.Resources>
				<DataTemplate x:Key="InteractiveDataDisplay.WPF.LineGraph">
					<StackPanel Orientation="Horizontal">
						<Line Width="15" Height="15" X1="0" Y1="0" X2="15" Y2="15" Stroke="{Binding Path=Stroke}" StrokeThickness="2"/>
						<TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}"/>
					</StackPanel>
				</DataTemplate>
			</d3:LegendItemsPanel.Resources>
		</d3:LegendItemsPanel>
	</d3:Chart.LegendContent>
	<Grid Name="lines"/>
</d3:Chart>
<Button x:Name="Btn_Cal" Content="开始计算" HorizontalAlignment="Left" Margin="540,0,0,25" VerticalAlignment="Bottom" Width="75" Click="Btn_Cal_Click"/>

但是,在设计过程中,发现,直接绘图会卡死按钮,只有等待绘图全部完成后,按钮才能再次点击,并且,在卡死过程中不会刷新图表控件,只有事件结束后,才显示最终折线图。这是由于button调用click事件后,click事件并未处理完成,无法再次生成事件,所以在处理中,click无法再次调用,而图表不会刷新,具体原由暂未明确。不过这并不妨碍实现我们想要的目标。
C#

namespace test
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<double> x = new List<double>();
        List<double> y = new List<double>();
        LineGraph lg = new LineGraph();

        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void Btn_Cal_Click(object sender, RoutedEventArgs e)
        {
            lines.Children.Clear();
            lines.Children.Add(lg);
            lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
            lg.Description = "Loss";
            lg.StrokeThickness = 2;
            Thread thread = new Thread(() =>
            {
                Process p = new Process();
                //设置要启动的应用程序
                p.StartInfo.FileName = @"run.exe";
                //是否使用操作系统shell启动
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                //输出信息
                p.StartInfo.RedirectStandardOutput = true;
                // 输出错误
                p.StartInfo.RedirectStandardError = true;
                //不显示程序窗口
                p.StartInfo.CreateNoWindow = true;
                //启动程序
                p.Start();
string strOuput;
                while ((strOuput = p.StandardOutput.ReadLine()) != null)
                {
                    if (strOuput.Substring(0, 4).Equals("loss"))
                    {
                        string[] str = strOuput.Split(':');
                        str = str[1].Split('-');
                        y.Add(double.Parse(str[1]));
                        x.Add(double.Parse(str[0]));
                		Dispatcher.Invoke(new Action(delegate
                		{
                    		lg.Plot(x, y);
                		}));
                    }
                }
                //等待程序执行完退出进程
                //p.WaitForExit();
                p.Close();
            });
            thread.Start();
        }
    }
}

使用线程,调用一个exe来获取(x,y),尽管解决了button不能弹起的问题(注意,线程中调用控件,需要以委托的方式,否则会产生异常),但是依然无法做到我们想要的实时绘图,反而会使,当前窗口卡死。于是,改动代码。

namespace test
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<double> x = new List<double>();
        List<double> y = new List<double>();
        LineGraph lg = new LineGraph();
        bool flag = true;

        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void Btn_Cal_Click(object sender, RoutedEventArgs e)
        {
			flag = true;
            lines.Children.Clear();
            lines.Children.Add(lg);
            lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
            lg.Description = "Loss";
            lg.StrokeThickness = 2;
            Thread thread = new Thread(() =>
            {
                Process p = new Process();
                //设置要启动的应用程序
                p.StartInfo.FileName = @"run.exe";
                //是否使用操作系统shell启动
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                //输出信息
                p.StartInfo.RedirectStandardOutput = true;
                // 输出错误
                p.StartInfo.RedirectStandardError = true;
                //不显示程序窗口
                p.StartInfo.CreateNoWindow = true;
                //启动程序
                p.Start();
string strOuput;
                while ((strOuput = p.StandardOutput.ReadLine()) != null)
                {
                    if (strOuput.Substring(0, 4).Equals("loss"))
                    {
                        string[] str = strOuput.Split(':');
                        str = str[1].Split('-');
                        y.Add(double.Parse(str[1]));
                        x.Add(double.Parse(str[0]));
                    }
                }
                //等待程序执行完退出进程
                //p.WaitForExit();
                p.Close();
                flag = false;
            });
            thread.Start();
            Thread thread2 = new Thread(DrawGraph);
            thread2.Start();
        }

        public void DrawGraph()
        {
            while(flag)
            {
                Thread.Sleep(1000);
                Dispatcher.Invoke(new Action(delegate
                {
                    lg.Plot(x, y);
                }));
            }
            MessageBox.Show("计算完毕");
        }
    }
}

调用一个新线程来执行绘图工作,每隔1s进行一次绘图。效果如下。在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值