Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Latest commit

History

116 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

RunProcessAsTask

Travis Build StatusAppVeyorCoveralls

GitHub issuesGitHub starsGitHub forksGitHub 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

NOTE: if you need to handle stdout/stderr as they happen (while the process is still running), you may want to use Process directly or look at the CliWrap project

Example Usages

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

Task<ProcessResults>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(varoutputinprocessResults.StandardOutput){Console.WriteLine("Output line: "+output);}Console.WriteLine("{0} lines of standard error",processResults.StandardError.Length);foreach(varerrorinprocessResults.StandardError){Console.WriteLine("Error line: "+error);}

Provide timeout for running process

publicasyncTaskRunCommandWithTimeout(stringfilename,stringarguments,TimeSpantimeout){varprocessStartInfo=newProcessStartInfo{FileName=filename,Arguments=arguments,};try{using(varcancellationTokenSource=newCancellationTokenSource(timeout)){varprocessResults=awaitProcessEx.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

publicasyncTaskShowLastMatchingCommit(stringregex){varlogProcessResults=awaitProcessEx.RunAsync("git.exe","log --pretty=oneline --all -n 1 -G"+regex);if(logProcessResults.ExitCode!=0)return;varstdoutSplit=logProcessResults.StandardOutput[0].Split(new[]{' '},2);varcommitHash=stdoutSplit[0];varcommitMessage=stdoutSplit[1];Console.WriteLine("Last commit matching {0} was {1} and had commit message {2}",regex,commitHash,commitMessage);varshowProcessResults=awaitProcessEx.RunAsync("git.exe","show --pretty=fuller "+commitHash);foreach(varstdoutLineinshowProcessResults.StandardOutput){Console.WriteLine("git show output: "+stdoutLine);}}

About

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

Resources

Stars

220 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages