在 Task.Run 中运行时未达到 Process.Exited [英] Process.Exited not reached when running in Task.Run

查看:23
本文介绍了在 Task.Run 中运行时未达到 Process.Exited的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 WPF 应用程序中使用 Task.Run 运行进程时,让 Process.Exited 事件触发时遇到问题.如果由于流程类限制而无法获得此 Exited 事件,我想在任务完成后更新 TextBox.Text.我试过 ContinueWith 也没有任何运气

I am having a problem getting the Process.Exited event to fire when I run the process using a Task.Run in a WPF app. If this getting this Exited event is out of the questions due to the process class limitation, I would like to to update the TextBox.Text when the task is complete. I tried ContinueWith without any luck either

async private void Select_Click(object sender, RoutedEventArgs e)
    {
        CancellationToken ct = new CancellationToken();
        CancellationTokenSource cts = new CancellationTokenSource();

        FolderBrowserDialog diag;
        TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
        Notification.Text = string.Empty;
        diag = new FolderBrowserDialog();
        var result = diag.ShowDialog();
        var istest = TestMode.IsChecked == true;

        if (result == System.Windows.Forms.DialogResult.OK)
        {
            var path = diag.SelectedPath;
            await Task.Run(() =>
            {
                var processStartInfo = new ProcessStartInfo()
                                {
                                    FileName = "cmd.exe",
                                    WindowStyle = ProcessWindowStyle.Hidden,
                                    RedirectStandardOutput = true,
                                    RedirectStandardError = true,
                                    RedirectStandardInput = true,
                                    UseShellExecute = false,
                                    CreateNoWindow = true
                                };
                var command="ping yahoo.com";

                var process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };
                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.StandardInput.WriteLineAsync(command);
                process.ErrorDataReceived += (p, @event) =>
                {
                    Dispatcher.InvokeAsync(() =>
                    {
                        Notification.AppendText(String.Format("Error {0} {1}", @event.Data, Environment.NewLine));
                    }, System.Windows.Threading.DispatcherPriority.Background);
                };
                process.OutputDataReceived += (p, @event) =>
                {
                    Dispatcher.InvokeAsync(() =>
                    {
                        Notification.AppendText(@event.Data + Environment.NewLine);
                    }, System.Windows.Threading.DispatcherPriority.Background);
                };
                process.WaitForExit();
                process.Exited += (@Sender, args) =>
                {
                    tcs.SetResult(process);
                    System.Windows.MessageBox.Show("Done");
                    process.Dispose();
                };
            });

        }

    }

所有其他事件都被触发而没有任何问题.谢谢

All of the other events are getting fired without any problems. Thank you

推荐答案

你在订阅 Exited 事件之前调用 WaitForExit,所以线程将在那里等待直到进程退出并且永远不会在进程退出之前订阅事件.当 WaitForExit 返回过程已经退出时,只有您订阅 Exited 事件,该事件永远不会再次触发.

You call WaitForExit before you subscribe Exited event, so thread will wait there till process exits and never subscribes the event before the process exits. When WaitForExit returns process is already exited, then only you subscribe Exited event which is never going to fire again.

所以在调用 WaitForExit 之前订阅 Exited 事件.

So subscribe Exited event before you call WaitForExit.

process.Exited += (@Sender, args) =>
{
    tcs.SetResult(process);
    System.Windows.MessageBox.Show("Done");
    process.Dispose();
};
process.WaitForExit();

这篇关于在 Task.Run 中运行时未达到 Process.Exited的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆