-
Notifications
You must be signed in to change notification settings - Fork 1
Add Process.IsRunning(int processId) static extension method
#10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
23c57c7
d8aeddf
f3bf13b
2708d82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using PowerKit.Extensions; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests; | ||
|
|
||
| public class ProcessExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void IsRunning_Running_Test() | ||
| { | ||
| // Act & assert | ||
| Process.IsRunning(Environment.ProcessId).Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task IsRunning_NotRunning_Test() | ||
| { | ||
| // Arrange | ||
| using var process = Process.Start(new ProcessStartInfo("dotnet", "--version") | ||
| { | ||
| RedirectStandardOutput = true | ||
| })!; | ||
|
|
||
| await process.WaitForExitAsync(); | ||
| var processId = process.Id; | ||
|
|
||
| // Act & assert | ||
| Process.IsRunning(processId).Should().BeFalse(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace PowerKit.Extensions; | ||
|
|
||
| internal static class ProcessExtensions | ||
| { | ||
| extension(Process) | ||
| { | ||
| /// <summary> | ||
| /// Checks whether the process identified by the specified ID is currently running. | ||
| /// </summary> | ||
| public static bool IsRunning(int processId) | ||
|
Tyrrrz marked this conversation as resolved.
|
||
| { | ||
| try | ||
| { | ||
| using var process = Process.GetProcessById(processId); | ||
| return !process.HasExited; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
|
Comment on lines
+14
to
+22
|
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.