private void ActiveStaticCommandOnExecutionStopped(object sender, EventArgs eventArgs) { var activeStaticCommand = (ActiveStaticCommand)sender; PotentialCommand potentialCommand; lock (_activeCommandsLock) { potentialCommand = ActiveCommands.FirstOrDefault(x => x.Value.CommandId == activeStaticCommand.CommandId).Key; if (potentialCommand == null) { return; } ActiveCommands.Remove(potentialCommand); } var serverConnection = (ServerConnection)_clientInfo.ServerConnection; lock (serverConnection.SendLock) { DynamicCommandFeedbackFactory.PushEvent(serverConnection.BinaryWriter, potentialCommand.CallbackId, ActivityType.Stopped, null); } }
private void ExecutePotentialCommand(PotentialCommand potentialCommand) { StaticCommand staticCommand; if (StaticCommands.TryGetValue(potentialCommand.CommandId, out staticCommand)) { var activeStaticCommand = staticCommand as ActiveStaticCommand; if (activeStaticCommand != null) { //create a new instance because the commands are session based activeStaticCommand = (ActiveStaticCommand)Activator.CreateInstance(activeStaticCommand.GetType()); activeStaticCommand.ExecutionStopped += ActiveStaticCommandOnExecutionStopped; //the command is automatically removed if (!_activeCommandStopScheduler.ExecuteActiveCommand(potentialCommand, activeStaticCommand)) { return; } lock (_activeCommandsLock) ActiveCommands.Add(potentialCommand, activeStaticCommand); var serverConnection = (ServerConnection)_clientInfo.ServerConnection; lock (serverConnection.SendLock) { DynamicCommandFeedbackFactory.PushEvent(serverConnection.BinaryWriter, potentialCommand.CallbackId, ActivityType.Active, null); } new Thread(() => { try { activeStaticCommand.Execute(new CommandParameter(potentialCommand.Parameter), null, _clientInfo); } catch (Exception) { ActiveStaticCommandOnExecutionStopped(activeStaticCommand, EventArgs.Empty); } }) { IsBackground = true }.Start(); } else { var feedbackFactory = new DynamicCommandFeedbackFactory((ServerConnection)_clientInfo.ServerConnection, potentialCommand.CallbackId); new Thread(() => { try { staticCommand.Execute(new CommandParameter(potentialCommand.Parameter), feedbackFactory, _clientInfo); } catch (Exception ex) { feedbackFactory.Failed("Critical error: " + ex.Message); } //that will execute anyways only if it wasn't pushed yet feedbackFactory.Succeeded(); }) { IsBackground = true }.Start(); } } }