-
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 3 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,33 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| 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 void IsRunning_NotRunning_Test() | ||
| { | ||
| // Arrange | ||
| using var process = Process.Start(new ProcessStartInfo("dotnet", "--version") | ||
| { | ||
| RedirectStandardOutput = true | ||
| })!; | ||
|
|
||
| process.WaitForExit(); | ||
| var processId = process.Id; | ||
|
|
||
|
Tyrrrz marked this conversation as resolved.
|
||
| // 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
|
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the async version of thsi method @copilot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 2708d82 — switched to
await process.WaitForExitAsync().