2 Commits

+35
View File
@@ -59,6 +59,41 @@ namespace ResourceMonitorService
await context.Response.WriteAsync(json);
});
app.MapPost("/api/kill-process", async context =>
{
try
{
var idStr = await new StreamReader(context.Request.Body).ReadToEndAsync();
int processId = Convert.ToInt32(idStr);
Process[] processes = Process.GetProcesses().Where(p => p.Id == processId).ToArray();
if (processes.Length > 0)
{
foreach (var process in processes)
{
try
{
process.Kill();
await context.Response.WriteAsync($"Process with ID {processId} has been killed.");
}
catch (Exception ex)
{
await context.Response.WriteAsync($"Error killing process with ID {processId}: {ex.Message}");
}
}
}
else
{
await context.Response.WriteAsync($"No process found with ID {processId}.");
}
}
catch (Exception ex)
{
await context.Response.WriteAsync($"An error occurred: {ex.Message}");
}
});
_ = app.RunAsync(stoppingToken);
await Task.Delay(Timeout.Infinite, stoppingToken);