【问题标题】:Process.Exited not reached when running in Task.Run在 Task.Run 中运行时未达到 Process.Exited
【发布时间】:2014-05-07 07:24:34
【问题描述】:

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

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();
                };
            });

        }

    }

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

【问题讨论】:

    标签: c# wpf task


    【解决方案1】:

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

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

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

    【讨论】:

    • 我确实试过了,但事件仍然没有触发。
    • @user3373870 你确定进程退出了吗?
    • 就是问题所在,进程没有退出。我必须强制进程(cmd.exe 退出)
    • @user3373870 This should help
    • @Sriram Sakthivel,使用 /C 参数,我不得不将退出事件修改为 process.Exited += (async(@Sender, args) => { tcs.TrySetResult(process); await Dispatcher.InvokeAsync(() => { Notification.AppendText("Completed"); }, System.Windows.Threading.DispatcherPriority.Background); process.Dispose(); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2020-04-18
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多