Coder Social home page Coder Social logo

charlieknoll / runprocessastask Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamesmanning/runprocessastask

0.0 2.0 0.0 85 KB

Simple wrapper around System.Diagnostics.Process to expose it as a System.Threading.Tasks.Task

License: MIT License

C# 100.00%

runprocessastask's Introduction

RunProcessAsTask

Travis Build Status AppVeyor Coveralls

GitHub issues GitHub stars GitHub forks GitHub license

NuGet

Simple wrapper around System.Diagnostics.Process to expose it as a System.Threading.Tasks.Task<ProcessResults>

Includes support for cancellation, timeout (via cancellation), and exposes the standard output, standard error, exit code, and run time of the process.

To install into your project:

PM> Install-Package RunProcessAsTask

Example Usages

Synchronous, just easier way of grabbing output / error / runtime for the process

var processResults = ProcessEx.RunAsync("git.exe", "pull").Result;

Console.WriteLine("Exit code: " + processResults.ExitCode);
Console.WriteLine("Run time: " + processResults.RunTime);

Console.WriteLine("{0} lines of standard output", processResults.StandardOutput.Length);
foreach (var output in processResults.StandardOutput)
{
    Console.WriteLine("Output line: " + output);
}

Console.WriteLine("{0} lines of standard error", processResults.StandardError.Length);
foreach (var error in processResults.StandardError)
{
    Console.WriteLine("Error line: " + error);
}

Provide timeout for running process

public async Task RunCommandWithTimeout(string filename, string arguments, TimeSpan timeout)
{
    var cancellationTokenSource = new CancellationTokenSource(timeout);

    var processStartInfo = new ProcessStartInfo
    {
        FileName = filename,
        Arguments = arguments,
    };
    try
    {
        var processResults = await ProcessEx.RunAsync(processStartInfo, cancellationTokenSource.Token);
    }
    catch (OperationCanceledException)
    {
        Console.WriteLine("Timeout of {0} hit while trying to run {1} {2}", timeout, filename, arguments);
    }
}

Run multiple commands with dependencies in an async fashion

public async Task ShowLastMatchingCommit(string regex)
{
    var logProcessResults = await ProcessEx.RunAsync("git.exe", "log --pretty=oneline --all -n 1 -G" + regex);
    if (logProcessResults.ExitCode != 0) return;

    var stdoutSplit = logProcessResults.StandardOutput[0].Split(new[] { ' ' }, 2);
    var commitHash = stdoutSplit[0];
    var commitMessage = stdoutSplit[1];
    Console.WriteLine("Last commit matching {0} was {1} and had commit message {2}", regex, commitHash, commitMessage);
    var showProcessResults = await ProcessEx.RunAsync("git.exe", "show --pretty=fuller " + commitHash);
    foreach (var stdoutLine in showProcessResults.StandardOutput)
    {
        Console.WriteLine("git show output: " + stdoutLine);
    }
}

runprocessastask's People

Contributors

brianfeucht avatar jamesmanning avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.