12

I found beneath code for execute some process without freezing UI. This code is executed when 'Start Work' button is pressed. And I think users would stop this work by 'Stop' button. So I found this article at MSDN.. https://msdn.microsoft.com/en-us/library/jj155759.aspx . But, It was hard that applying this CancellationToken at this code.. Anyone can help this problem?

I use public static async Task<int> RunProcessAsync(string fileName, string args) method only.

Code (From https://stackoverflow.com/a/31492250):

public static async Task<int> RunProcessAsync(string fileName, string args)
{
    using (var process = new Process
    {
        StartInfo =
        {
            FileName = fileName, Arguments = args,
            UseShellExecute = false, CreateNoWindow = true,
            RedirectStandardOutput = true, RedirectStandardError = true
        },
        EnableRaisingEvents = true
    })
    {
        return await RunProcessAsync(process).ConfigureAwait(false);
    }
}

// This method is used only for internal function call.
private static Task<int> RunProcessAsync(Process process)
{
    var tcs = new TaskCompletionSource<int>();

    process.Exited += (s, ea) => tcs.SetResult(process.ExitCode);
    process.OutputDataReceived += (s, ea) => Console.WriteLine(ea.Data);
    process.ErrorDataReceived += (s, ea) => Console.WriteLine("ERR: " + ea.Data);

    bool started = process.Start();
    if (!started)
    {
        //you may allow for the process to be re-used (started = false) 
        //but I'm not sure about the guarantees of the Exited event in such a case
        throw new InvalidOperationException("Could not start process: " + process);
    }

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    return tcs.Task;
}

Usage :

var cancelToken = new CancellationTokenSource();
int returnCode = async RunProcessAsync("python.exe", "foo.py", cancelToken.Token);
if (cancelToken.IsCancellationRequested) { /* something */ }

When the start button clicked, it starts some python script. When script is running and user wants to stop it, user presses stop button. Then program executes below code.

cancelToken.Cancel();

Thank you very much for reading this question.

5
  • Can I know why my question gave that -1 without any answer or comments?
    – youngminz
    Dec 10, 2015 at 10:51
  • 1
    What do you want the CancellationToken to do? Kill the process? Dec 10, 2015 at 11:58
  • @StephenCleary: Thank you for comment! Yes, What I want to do is Terminate program immediately.
    – youngminz
    Dec 10, 2015 at 12:19
  • 2
    This question is not clear at all. There's not even a CancellationToken in the code you posted! Never mind do I see any sort of problem statement, other than maybe "it was hard". Please provide a good minimal reproducible example that reliably reproduces whatever problem you are having. Also provide a precise description of your problem: explain what the code does now and what you want it to do instead. Dec 11, 2015 at 7:57
  • @PeterDuniho I added some short usage code. Thanks for advise!
    – youngminz
    Feb 5, 2016 at 9:30

2 Answers 2

24

The simple answer is that you can just call process.Kill() when the token is canceled:

cancellationToken.Register(() => process.Kill());

But there are two problems with this:

  1. If you attempt to kill a process that doesn't exist yet or that has already terminated, you get an InvalidOperationException.
  2. If you don't Dispose() the CancellationTokenRegistration returned from Register(), and the CancellationTokenSource is long-lived, you have a memory leak, since the registrations will stay in memory as long as the CancellationTokenSource.

Depending on your requirements, and your desire for clean code (even at the cost of complexity) it may be okay to ignore problem #2 and work around problem #1 by swallowing the exception in a catch.

2
  • Should the call to the Register() method come before are after the call to process.Start()? How about in relation to the call to the Exited event? Can post as a separate question if recommended. My example is like your answer to stackoverflow.com/questions/10788982/… Jun 20, 2016 at 19:14
  • 2
    you can also do the same this with less parenthesis: cancellationToken.Register(process.Kill); Feb 11, 2019 at 14:54
2

It's quite simple now:

process.WaitForExitAsync(token);
2
  • await process.WaitForExitAsync(token); for completeness Jan 13, 2023 at 21:37
  • 1
    In the comments, the OP indicated that the cancellation should result in the external process being terminated, which constitutes the meat of @svick's answer. Cancelling the token passed to WaitForExitAsync() will not kill the external process, it just stops the currently running program from waiting for it. The CancellationTokenRegistration described above is still required.
    – Marc L.
    Nov 1, 2023 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.