{"id":19100370,"url":"https://github.com/jamesmanning/RunProcessAsTask","last_synced_at":"2025-04-18T17:33:19.618Z","repository":{"id":5763590,"uuid":"6976652","full_name":"jamesmanning/RunProcessAsTask","owner":"jamesmanning","description":"Simple wrapper around System.Diagnostics.Process to expose it as a System.Threading.Tasks.Task","archived":false,"fork":false,"pushed_at":"2021-11-04T11:44:08.000Z","size":145,"stargazers_count":216,"open_issues_count":4,"forks_count":37,"subscribers_count":24,"default_branch":"master","last_synced_at":"2024-10-02T00:43:13.506Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamesmanning.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-03T04:31:14.000Z","updated_at":"2024-08-19T08:00:55.000Z","dependencies_parsed_at":"2022-07-19T00:17:02.605Z","dependency_job_id":null,"html_url":"https://github.com/jamesmanning/RunProcessAsTask","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesmanning%2FRunProcessAsTask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesmanning%2FRunProcessAsTask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesmanning%2FRunProcessAsTask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesmanning%2FRunProcessAsTask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamesmanning","download_url":"https://codeload.github.com/jamesmanning/RunProcessAsTask/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783094,"owners_count":17201904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T03:52:47.960Z","updated_at":"2024-11-09T03:53:54.064Z","avatar_url":"https://github.com/jamesmanning.png","language":"C#","funding_links":[],"categories":["C#","others"],"sub_categories":[],"readme":"RunProcessAsTask\n================\n\n[![Travis Build Status](https://travis-ci.org/jamesmanning/RunProcessAsTask.svg?branch=master)](https://travis-ci.org/jamesmanning/RunProcessAsTask)\n[![AppVeyor](https://img.shields.io/appveyor/ci/jamesmanning/RunProcessAsTask.svg)](https://ci.appveyor.com/project/jamesmanning/RunProcessAsTask)\n[![Coveralls](https://img.shields.io/coveralls/jamesmanning/RunProcessAsTask.svg)](https://coveralls.io/github/jamesmanning/RunProcessAsTask)\n\n[![GitHub issues](https://img.shields.io/github/issues/jamesmanning/RunProcessAsTask.svg)](https://github.com/jamesmanning/RunProcessAsTask/issues)\n[![GitHub stars](https://img.shields.io/github/stars/jamesmanning/RunProcessAsTask.svg)](https://github.com/jamesmanning/RunProcessAsTask/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/jamesmanning/RunProcessAsTask.svg)](https://github.com/jamesmanning/RunProcessAsTask/network)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/jamesmanning/RunProcessAsTask/master/LICENSE)\n\n[![NuGet](https://img.shields.io/nuget/v/RunProcessAsTask.svg)](https://www.nuget.org/packages/RunProcessAsTask/)\n\nSimple wrapper around [System.Diagnostics.Process](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx) to expose it as a [System.Threading.Tasks.Task](http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx)\u003c[ProcessResults](https://github.com/jamesmanning/RunProcessAsTask/blob/master/src/RunProcessAsTask/ProcessResults.cs)\u003e\n\nIncludes support for cancellation, timeout (via cancellation), and exposes the standard output, standard error, exit code, and run time of the process.\n\nTo install into your project:\n\n```powershell\nPM\u003e Install-Package RunProcessAsTask\n```\n\nNOTE: 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](https://github.com/Tyrrrz/CliWrap) project\n\n# Example Usages\n\n## Synchronous, just easier way of grabbing output / error / runtime for the process\n\n```csharp\nTask\u003cProcessResults\u003e processResults = ProcessEx.RunAsync(\"git.exe\", \"pull\").Result;\n\nConsole.WriteLine(\"Exit code: \" + processResults.ExitCode);\nConsole.WriteLine(\"Run time: \" + processResults.RunTime);\n\nConsole.WriteLine(\"{0} lines of standard output\", processResults.StandardOutput.Length);\nforeach (var output in processResults.StandardOutput)\n{\n    Console.WriteLine(\"Output line: \" + output);\n}\n\nConsole.WriteLine(\"{0} lines of standard error\", processResults.StandardError.Length);\nforeach (var error in processResults.StandardError)\n{\n    Console.WriteLine(\"Error line: \" + error);\n}\n```\n\n## Provide timeout for running process\n\n```csharp\npublic async Task RunCommandWithTimeout(string filename, string arguments, TimeSpan timeout)\n{\n    var processStartInfo = new ProcessStartInfo\n    {\n        FileName = filename,\n        Arguments = arguments,\n    };\n    try\n    {\n        using (var cancellationTokenSource = new CancellationTokenSource(timeout))\n        {\n            var processResults = await ProcessEx.RunAsync(processStartInfo, cancellationTokenSource.Token);\n        }\n    }\n    catch (OperationCanceledException)\n    {\n        Console.WriteLine(\"Timeout of {0} hit while trying to run {1} {2}\", timeout, filename, arguments);\n    }\n}\n```\n\n## Run multiple commands with dependencies in an async fashion\n\n```csharp\npublic async Task ShowLastMatchingCommit(string regex)\n{\n    var logProcessResults = await ProcessEx.RunAsync(\"git.exe\", \"log --pretty=oneline --all -n 1 -G\" + regex);\n    if (logProcessResults.ExitCode != 0) return;\n\n    var stdoutSplit = logProcessResults.StandardOutput[0].Split(new[] { ' ' }, 2);\n    var commitHash = stdoutSplit[0];\n    var commitMessage = stdoutSplit[1];\n    Console.WriteLine(\"Last commit matching {0} was {1} and had commit message {2}\", regex, commitHash, commitMessage);\n    var showProcessResults = await ProcessEx.RunAsync(\"git.exe\", \"show --pretty=fuller \" + commitHash);\n    foreach (var stdoutLine in showProcessResults.StandardOutput)\n    {\n        Console.WriteLine(\"git show output: \" + stdoutLine);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesmanning%2FRunProcessAsTask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamesmanning%2FRunProcessAsTask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesmanning%2FRunProcessAsTask/lists"}