Add API endpoint to kill process and update README
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user