private int ExecuteCommand( ICommand TargetCommand, string[] Arguments, bool bWaitIfNeeded) { if ( TargetCommand is ISingleInstanceCommand ) { bool bAllowExecute = Monitor.TryEnter( TargetCommand.GetType(), bWaitIfNeeded ? Timeout.Infinite : 0 ); if ( ! bAllowExecute ) { CommandException ExecutionError = new CommandException(); throw ExecutionError; } } int CommandStatus; System.Exception ProcessError = null; SetInputMode( InputMode.Redirected ); _CommandList.Add( TargetCommand ); CommandArgument[] commandArguments = GenerateArguments( Arguments ); try { ICommandResult commandResult; CommandStatus = TargetCommand.PerformCommand( commandArguments, out commandResult ); } catch ( System.Exception ProcessException) { CommandStatus = 0; ProcessError = ProcessException; } bool bRequiresParent = false; bool bExternal = false; try { bRequiresParent = TargetCommand.RequiresParentTerminal(); } catch { } if ( TargetCommand is ExternalCommand ) { bExternal = true; if ( ! bRequiresParent ) { ( (ExternalCommand) TargetCommand ).Disconnect(); } } if ( ! bExternal || ! bRequiresParent ) { _CommandList.Remove( TargetCommand ); } if ( ! bRequiresParent ) { SetInputMode( InputMode.Native ); } if ( TargetCommand is ISingleInstanceCommand ) { Monitor.Exit( TargetCommand.GetType() ); } if ( null != ProcessError ) { throw ProcessError; } return CommandStatus; }