SEBWIN-355: Improved process factory to ensure process termination in between .NET and WMI data access is handled gracefully.

This commit is contained in:
dbuechel 2019-12-16 11:49:59 +01:00
parent eb3a87016e
commit 8953313642

View file

@ -40,10 +40,12 @@ namespace SafeExamBrowser.WindowsApi
foreach (var process in running) foreach (var process in running)
{ {
var name = names.FirstOrDefault(n => n.processId == process.Id).name; if (names.Any(n => n.processId == process.Id))
var originalName = names.FirstOrDefault(n => n.processId == process.Id).originalName; {
var (_, name, originalName) = names.First(n => n.processId == process.Id);
processes.Add(new Process(process, name, originalName, LoggerFor(process, name))); processes.Add(new Process(process, name, originalName, LoggerFor(process, name)));
}
} }
return processes; return processes;
@ -74,16 +76,19 @@ namespace SafeExamBrowser.WindowsApi
public bool TryGetById(int id, out IProcess process) public bool TryGetById(int id, out IProcess process)
{ {
var raw = System.Diagnostics.Process.GetProcesses().FirstOrDefault(p => p.Id == id);
process = default(IProcess); process = default(IProcess);
if (raw != default(System.Diagnostics.Process)) try
{ {
var raw = System.Diagnostics.Process.GetProcessById(id);
var (name, originalName) = LoadProcessNamesFor(raw); var (name, originalName) = LoadProcessNamesFor(raw);
process = new Process(raw, name, originalName, LoggerFor(raw, name)); process = new Process(raw, name, originalName, LoggerFor(raw, name));
} }
catch (Exception e)
{
logger.Error($"Failed to get process with ID = {id}!", e);
}
return process != default(IProcess); return process != default(IProcess);
} }