void shellControl_CommandEntered(object sender, CommandEnteredEventArgs e) { string command = e.Command; if (command == "/") { command = lastCommand; } if (processCommand(command)) { if (command != "/") lastCommand = command; } }
/// <summary> /// Shells the control1 on command entered. /// </summary> /// <param name="sender">The sender.</param> /// <param name="commandEnteredEventArgs">The <see cref="CommandEnteredEventArgs"/> instance containing the event data.</param> private async void ShellControl1OnCommandEntered(object sender, CommandEnteredEventArgs commandEnteredEventArgs) { if (string.IsNullOrWhiteSpace(commandEnteredEventArgs.Command)) { return; } var async = false; try { // Create a ConsoleCommand instance: var cmd = new ConsoleCommand(commandEnteredEventArgs.Command.Trim().Replace("\t", "")); // Execute the command: ConsoleExecuteResult result; if (ConsoleExecute.IsAsync(cmd, _commandLibraries)) { Interlocked.Increment(ref _asyncCount); async = true; result = await ConsoleExecute.ExecuteAsync(cmd, _commandLibraries).ConfigureAwait(false); } else { result = ConsoleExecute.Execute(cmd, _commandLibraries); } // Write out the result: WriteToConsole(result.Message); if (result.Action != null && result.Action.Action == ConsoleExecuteActions.Exit) { //If we exit here and async tasks are running, we won't ever finish them. That's because they are tied to the shell's command event... if (Interlocked.Read(ref _asyncCount) == 0) { shellControl1.CommandEntered -= ShellControl1OnCommandEntered; Close(); } else { WriteToConsole($"Async tasks are still running {Interlocked.Read(ref _asyncCount)}"); } } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.StatusUri) { queueStatusControl1.Display(result.Action.Target); } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.StartProcess) { Process.Start(result.Action.Target); } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.StartMacro) { ConsoleExecute.StartMacroCapture(); } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.CancelMacro) { ConsoleExecute.CancelMacroCapture(); } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.SaveMacro) { ConsoleExecute.SaveMacro(Path.Combine(Path.GetDirectoryName(_commandAssembly.Location) + @"\Macro\", result.Action.Target)); } else if (result.Action != null && result.Action.Action == ConsoleExecuteActions.RunMacro) { shellControl1.PrintLine(); foreach (ConsoleExecuteResult command in ConsoleExecute.RunMacro(Path.Combine(Path.GetDirectoryName(_commandAssembly.Location) + @"\Macro\", result.Action.Target))) { if (command.Action.Action == ConsoleExecuteActions.RunCommand) { shellControl1.SendCommand(command.Action.Target); } } } } catch (Exception ex) { WriteToConsole(ex.ToString()); } finally { if (async) { Interlocked.Decrement(ref _asyncCount); } } }