如何取消在C#中启动进程的异步任务?

时间:2016-08-11 18:26:29

标签: c# asynchronous task taskcompletionsource

目前我的代码从svn运行结账,并使用两个任务将stdout和stderr重定向到文本框,如下所示。我希望能够在用户单击StopButton并且能够取消下载时立即取消该任务。据我所知,如果我改变我的cmd.exe命令来说出诸如" Pause"之类的东西,它继续运行直到用户点击某些东西,我可以用StopButton取消这个命令。我是C#的新手并试图理解为什么我可以取消该命令而不是svn下载。

这是我应该实现我的代码还是应该尝试不同的方法?

非常感谢你的帮助!我真的很感激!

   public Form2()
    {
        InitializeComponent();

    }

    private TaskCompletionSource<bool> _cancelTask;

    private async Task RunProcess()
    {
        Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = "svn download"
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = false,
            }
        };


        Console.Write(URL);
        process.Start();

        Task readerTasks = Task.WhenAll(
            ConsumeReader(process.StandardError),
            ConsumeReader(process.StandardOutput));

        Task completedTask = await Task.WhenAny(readerTasks, _cancelTask.Task);

        if(completedTask == _cancelTask.Task)
        {
            process.Kill();
            await readerTasks;
            throw new TaskCanceledException(_cancelTask.Task);
        }
    }

    private async Task ConsumeReader(TextReader reader)
    {
        char[] text = new char[512];
        int cch;

        while ((cch = await reader.ReadAsync(text, 0, text.Length)) > 0)
        {
            textBox2.AppendText(new string(text, 0, cch));
        }
    }

    private async void Submit_Click(object sender, EventArgs e)
    {

        Submit.Enabled = false;
        StopButton.Enabled = true;
        _cancelTask = new TaskCompletionSource<bool>();

        try
        {
            await RunProcess(cts.Token);
        }
        catch (OperationCanceledException)
        {
            MessageBox.Show("The operation was cancelled");
        }
        finally
        {
            _cancelTask = null;
            Submit.Enabled = true;
            StopButton.Enabled = false;
        }
    }

    private void StopButton_Click(object sender, EventArgs e)
    {
        _cancelTask.SetCanceled();
    }

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,不确定这是否是最好的,但你可以启动另一个进程来启动命令行命令taskkill来杀死所有正在运行的.exe。我现在杀死cmd.exe(父)和从cmd.exe调用的svn.exe(子)!

这只有在您知道从父项启动的.exes时才有用,如果创建了更多子项,最好找到一个不同的解决方案来递归查找子进程。

    if(completedTask == _cancelTask.Task)
    {
        //process.Kill(); This would only kill the CMD.exe
        Process exit = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    //Arguments = "/K Pause",
                    Arguments = "/C taskkill /f /im svn.exe & taskkill /f /im cmd.exe",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = false,
                }
            };
            exit.Start();
        await readerTasks;
        throw new TaskCanceledException(_cancelTask.Task);
    }