/// <summary> /// The main execution logic of all scripts. /// </summary> internal void MainLoop() { _running = true; // Wait for domain to run scripts _continueEvent.WaitOne(); // Run main loop while (_running) { Tuple <bool, WinForms.KeyEventArgs> keyevent = null; // Process events while (_keyboardEvents.TryDequeue(out keyevent)) { try { if (keyevent.Item1) { KeyDown?.Invoke(this, keyevent.Item2); } else { KeyUp?.Invoke(this, keyevent.Item2); } } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, false)); break; } } try { Tick?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); Abort(); break; } // Yield execution to next tick Wait(_interval); } }
/// <summary> /// Aborts execution of this <see cref="Script"/>. /// </summary> public void Abort() { try { Aborted?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); } _running = false; _waitEvent.Set(); if (_thread != null) { _thread.Abort(); _thread = null; ScriptDomain.OnAbortScript(this); } }