WPF 为什么使用了BeginInvoke依然卡死了?

问题遇到的现象和发生背景

我想用WPF运行命令行,然后实时获取命令行内容,并更新UI(TextBox)

问题相关代码,请勿粘贴截图
    public partial class MainWindow : Window
    {
        Process p;
        public MainWindow()
        {
            InitializeComponent();
            p = new Process();
        }

        private void importButton(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate    //开启新线程
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.Start();//启动程序
                p.OutputDataReceived += P_OutputDataReceived;//添加事件委托
                p.WaitForExit();//等待程序执行完
                p.Close();//退出进程
            })).Wait() ;

        }

        private void P_OutputDataReceived(object sender, DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                string filePath = outLine.Data;                               //实时获取内容

                Bean bean = (Bean)this.FindResource("textBean");
                bean.FilePath += filePath;                                            //改变FilePath,触发改变事件
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            p.StandardInput.WriteLine("c:");//向已开启的命令行输入内容
        }
    }
运行结果及报错内容

但是运行后就卡死,能详细解答和指导一下么?

我的解答思路和尝试过的方法

我试过好多网上启用多线程的方法,但是没有什么好用的

我想要达到的结果

希望能完成功能

题主是在主线程中执行的cmd命令,和下面这篇文章一样


importButton应该开一个线程执行cmd命令然后在P_OutputDataReceived中this.Dispatcher.BeginInvoke更新数据

img