Add API endpoint to kill process and update README

This commit is contained in:
Din VM
2025-01-25 12:58:13 +08:00
parent a2ea577efb
commit e19e1e0137
20 changed files with 72 additions and 10 deletions
+45
View File
@@ -50,11 +50,56 @@ namespace ResourceMonitorService
await context.Response.WriteAsync(json);
});
app.MapPost("/api/kill-process", async context =>
{
await Results.Ok($"Macam celaka Process ni. ").ExecuteAsync(context);
/* try
{
int pid;
if (!int.TryParse(context.Request.Query["id"], out pid))
{
await Results.BadRequest("Invalid process ID.").ExecuteAsync(context);
return;
}
var result = await KillProcessByIdAsync(pid); // Ensure KillProcessByIdAsync returns a Task<bool>
if (result)
{
await Results.Ok($"Process with PID {pid} has been terminated successfully.").ExecuteAsync(context);
}
else
{
await Results.NotFound($"No process found with PID {pid}.").ExecuteAsync(context);
}
}
catch (Exception ex)
{
// Log the exception or handle it as needed
await Results.Problem(ex.Message).ExecuteAsync(context);
} */
});
app.RunAsync(stoppingToken);
await Task.Delay(Timeout.Infinite, stoppingToken);
}
private async Task<bool> KillProcessByIdAsync(int pid)
{
using (Process process = Process.GetProcessById(pid))
{
if (process != null)
{
await Task.Run(() => process.Kill());
return true;
}
else
{
return false;
}
}
}
private object GetComputerInfo()
{
return new