Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions PowerKit.Tests/ProcessExtensionsTests.cs
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;

Comment thread
Tyrrrz marked this conversation as resolved.
// Act & assert
Process.IsRunning(processId).Should().BeFalse();
}
}
25 changes: 25 additions & 0 deletions PowerKit/Extensions/ProcessExtensions.cs
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)
Comment thread
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
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bare catch swallows all exceptions, including ones that typically should not be suppressed (and makes debugging harder if unexpected failures occur). Prefer catching the specific exceptions that represent 'not running / cannot be queried' (e.g., process not found / access denied) and let other exceptions surface, or use an exception filter to return false only for the expected cases.

Copilot uses AI. Check for mistakes.
}
}
}
Loading