SEBWIN-312: Fixed event handling for external application instances.
This commit is contained in:
parent
8dacc208ea
commit
4f930a26d8
2 changed files with 40 additions and 27 deletions
|
@ -58,11 +58,11 @@ namespace SafeExamBrowser.Applications
|
|||
if (instances.Any())
|
||||
{
|
||||
logger.Info("Terminating application...");
|
||||
}
|
||||
|
||||
foreach (var instance in instances)
|
||||
{
|
||||
instance.Terminate();
|
||||
foreach (var instance in instances)
|
||||
{
|
||||
instance.Terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,12 +50,7 @@ namespace SafeExamBrowser.Applications
|
|||
|
||||
public void Initialize()
|
||||
{
|
||||
process.Terminated += Process_Terminated;
|
||||
|
||||
timer = new Timer(ONE_SECOND);
|
||||
timer.Elapsed += Timer_Elapsed;
|
||||
timer.Start();
|
||||
|
||||
InitializeEvents();
|
||||
logger.Info("Initialized application instance.");
|
||||
}
|
||||
|
||||
|
@ -64,37 +59,55 @@ namespace SafeExamBrowser.Applications
|
|||
const int MAX_ATTEMPTS = 5;
|
||||
const int TIMEOUT_MS = 500;
|
||||
|
||||
timer.Elapsed -= Timer_Elapsed;
|
||||
timer?.Stop();
|
||||
|
||||
var terminated = process.HasTerminated;
|
||||
|
||||
for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++)
|
||||
if (!terminated)
|
||||
{
|
||||
terminated = process.TryClose(TIMEOUT_MS);
|
||||
}
|
||||
FinalizeEvents();
|
||||
|
||||
for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++)
|
||||
{
|
||||
terminated = process.TryKill(TIMEOUT_MS);
|
||||
}
|
||||
for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++)
|
||||
{
|
||||
terminated = process.TryClose(TIMEOUT_MS);
|
||||
}
|
||||
|
||||
if (terminated)
|
||||
{
|
||||
logger.Info("Successfully terminated application instance.");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Warn("Failed to terminate application instance!");
|
||||
for (var attempt = 0; attempt < MAX_ATTEMPTS && !terminated; attempt++)
|
||||
{
|
||||
terminated = process.TryKill(TIMEOUT_MS);
|
||||
}
|
||||
|
||||
if (terminated)
|
||||
{
|
||||
logger.Info("Successfully terminated application instance.");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Warn("Failed to terminate application instance!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Process_Terminated(int exitCode)
|
||||
{
|
||||
logger.Info($"Application instance has terminated with exit code {exitCode}.");
|
||||
FinalizeEvents();
|
||||
Terminated?.Invoke(Id);
|
||||
}
|
||||
|
||||
private void InitializeEvents()
|
||||
{
|
||||
timer = new Timer(ONE_SECOND);
|
||||
timer.Elapsed += Timer_Elapsed;
|
||||
timer.Start();
|
||||
process.Terminated += Process_Terminated;
|
||||
}
|
||||
|
||||
private void FinalizeEvents()
|
||||
{
|
||||
timer.Elapsed -= Timer_Elapsed;
|
||||
timer.Stop();
|
||||
process.Terminated -= Process_Terminated;
|
||||
}
|
||||
|
||||
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
var success = process.TryGetWindowTitle(out var title);
|
||||
|
|
Loading…
Reference in a new issue