SEBWIN-361: Fixed concurrency issue when terminating external applications and ensured termination operation continues even with failure.
This commit is contained in:
		
							parent
							
								
									07bb78e637
								
							
						
					
					
						commit
						2b7ff4561a
					
				
					 2 changed files with 22 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -81,6 +81,7 @@ namespace SafeExamBrowser.Applications
 | 
			
		|||
			{
 | 
			
		||||
				logger.Info("Starting application...");
 | 
			
		||||
				InitializeInstance(processFactory.StartNew(executablePath));
 | 
			
		||||
				logger.Info("Successfully started application.");
 | 
			
		||||
			}
 | 
			
		||||
			catch (Exception e)
 | 
			
		||||
			{
 | 
			
		||||
| 
						 | 
				
			
			@ -92,18 +93,28 @@ namespace SafeExamBrowser.Applications
 | 
			
		|||
		{
 | 
			
		||||
			applicationMonitor.InstanceStarted -= ApplicationMonitor_InstanceStarted;
 | 
			
		||||
 | 
			
		||||
			lock (@lock)
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				if (instances.Any() && !settings.AllowRunning)
 | 
			
		||||
				lock (@lock)
 | 
			
		||||
				{
 | 
			
		||||
					logger.Info("Terminating application...");
 | 
			
		||||
 | 
			
		||||
					foreach (var instance in instances)
 | 
			
		||||
					if (instances.Any() && !settings.AllowRunning)
 | 
			
		||||
					{
 | 
			
		||||
						instance.Terminate();
 | 
			
		||||
						logger.Info($"Terminating application with {instances.Count} instance(s)...");
 | 
			
		||||
 | 
			
		||||
						foreach (var instance in instances)
 | 
			
		||||
						{
 | 
			
		||||
							instance.Terminated -= Instance_Terminated;
 | 
			
		||||
							instance.Terminate();
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						logger.Info("Successfully terminated application.");
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch (Exception e)
 | 
			
		||||
			{
 | 
			
		||||
				logger.Error($"Failed to terminate application!", e);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private void ApplicationMonitor_InstanceStarted(Guid applicationId, IProcess process)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -66,7 +66,11 @@ namespace SafeExamBrowser.Applications
 | 
			
		|||
 | 
			
		||||
			var terminated = process.HasTerminated;
 | 
			
		||||
 | 
			
		||||
			if (!terminated)
 | 
			
		||||
			if (terminated)
 | 
			
		||||
			{
 | 
			
		||||
				logger.Info("Application instance is already terminated.");
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				FinalizeEvents();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue