/// <summary> /// Aborts execution of this script. /// </summary> public void Abort() { IsRunning = false; waitEvent.Release(); try { Aborted?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); } if (thread != null) { thread.Abort(); thread = null; Log.Message(Log.Level.Info, "Aborted script ", Name, "."); } // Unregister any console commands attached to this script var console = AppDomain.CurrentDomain.GetData("Console") as Console; console?.UnregisterCommands(ScriptInstance.GetType()); }
/// <summary> /// The main execution logic of all scripts. /// </summary> void MainLoop() { IsRunning = true; // Wait for script domain to continue this script continueEvent.Wait(); while (IsRunning) { // Process keyboard events while (keyboardEvents.TryDequeue(out Tuple <bool, KeyEventArgs> ev)) { try { if (!ev.Item1) { KeyUp?.Invoke(this, ev.Item2); } else { KeyDown?.Invoke(this, ev.Item2); } } catch (ThreadAbortException) { // Stop main loop immediately on a thread abort exception return; } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, false)); break; // Break out of key event loop, but continue to run script } } try { Tick?.Invoke(this, EventArgs.Empty); } catch (ThreadAbortException) { // Stop main loop immediately on a thread abort exception return; } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); // An exception during tick is fatal, so abort the script and stop main loop Abort(); return; } // Yield execution to next tick Wait(Interval); } }
/// <summary> /// Aborts execution of this script. /// </summary> public void Abort() { IsRunning = false; try { Aborted?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); } waitEvent.Release(); if (thread != null) { Log.Message(Log.Level.Warning, "Aborted script ", Name, "."); thread.Abort(); thread = null; } }