Bug 492182 - Add Terminate and Relaunch from Configurations, Relaunch
actions and Context based launch

Change-Id: Ie4828a93ec219dd9a5f1298451e42f1b6a765ecb
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/DebugUITools.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/DebugUITools.java
index df2c46e..e0474df 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/DebugUITools.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/DebugUITools.java
@@ -13,6 +13,7 @@
 
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.Set;
 
 import org.eclipse.core.commands.ExecutionEvent;
@@ -898,14 +899,22 @@
 			ILaunch[] launches = launchManager.getLaunches();
 			for (ILaunch iLaunch : launches) {
 				if (configuration.contentsEqual(iLaunch.getLaunchConfiguration())) {
-					try {
-						iLaunch.terminate();
-					} catch (DebugException e) {
-						DebugUIPlugin.log(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), NLS.bind(ActionMessages.TerminateAndLaunchFailure, iLaunch.getLaunchConfiguration().getName()), e));
+					synchronized (fgLaunchList) {
+						fgLaunchList.add(iLaunch);
 					}
 				}
 			}
 		}
+		if (!fgLaunchList.isEmpty()) {
+			Thread t = new Thread(fgLaunchTerminate);
+			t.start();
+			try {
+				t.join();
+			} catch (InterruptedException e1) {
+				DebugUIPlugin.log(e1);
+				return;
+			}
+		}
 		boolean launchInBackground = true;
 		try {
 			launchInBackground = configuration.getAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
@@ -919,6 +928,29 @@
 		}
 	}
 
+
+	private static Set<ILaunch> fgLaunchList = new LinkedHashSet<>();
+	private static Runnable fgLaunchTerminate = new Runnable() {
+		@Override
+		public void run() {
+			String launchConfigName = null;
+			Set<ILaunch> launchList;
+			try {
+				synchronized (fgLaunchList) {
+					launchList = new LinkedHashSet<>(fgLaunchList);
+					fgLaunchList.clear();
+				}
+				for (ILaunch iLaunch : launchList) {
+					launchConfigName = iLaunch.getLaunchConfiguration().getName();
+					iLaunch.terminate();
+				}
+
+			} catch (DebugException e) {
+				DebugUIPlugin.log(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), NLS.bind(ActionMessages.TerminateAndLaunchFailure, launchConfigName), e));
+			}
+		}
+	};
+
 	
 	/**
 	 * Builds the workspace according to current preference settings, and launches