Process.Exited not reached when running in Task.Run

Deadly 提交于 2019-12-13 00:26:20

问题


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


回答1:


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.

So subscribe Exited event before you call WaitForExit.

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


来源:https://stackoverflow.com/questions/23511343/process-exited-not-reached-when-running-in-task-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!