コード例 #1
0
        public void Stop()
        {
            _cancellationToken?.Cancel();

            if (Thread == null || !Thread.IsAlive)
            {
                return;
            }

            try
            {
                StopWatch.Stop();

                int diff = 50 - (int)StopWatch.ElapsedMilliseconds;

                if (diff > 0)
                {
                    Thread.Sleep(diff);
                }

                Thread?.Interrupt();
                Thread?.Abort();
                Thread?.Join(100);
                MacroManager.GetInstance().Replay = false;
                MacroManager.GetInstance().OnMacroStopped();
            }
            catch (ThreadStateException e)
            {
                UO.Commands.SystemMessage(string.Format(Strings.Macro_error___0_, e.Message));
            }
        }
コード例 #2
0
        public static MacroManager GetInstance()
        {
            // ReSharper disable once InvertIf
            if (_instance == null)
            {
                lock ( _lock )
                {
                    if (_instance != null)
                    {
                        return(_instance);
                    }

                    _instance = new MacroManager();
                    return(_instance);
                }
            }

            return(_instance);
        }
コード例 #3
0
        public void Stop()
        {
            _cancellationToken?.Cancel();

            if (Thread == null || !Thread.IsAlive)
            {
                return;
            }

            try
            {
                Thread?.Interrupt();
                Thread?.Abort();
                Thread?.Join(100);
                MacroManager.GetInstance().Replay = false;
            }
            catch (ThreadStateException e)
            {
                UO.Commands.SystemMessage(string.Format(Strings.Macro_error___0_, e.Message));
            }
        }
コード例 #4
0
        public void Execute(MacroEntry macro)
        {
            _macro = macro;

            if (Thread != null && Thread.IsAlive)
            {
                Stop();
            }

            MainCommands.SetQuietMode(Options.CurrentOptions.DefaultMacroQuietMode);

            _cancellationToken = new CancellationTokenSource();

            if (_importCache == null)
            {
                _importCache = InitializeImports(_engine);
            }

            ScriptSource source = _engine.CreateScriptSourceFromString(_macro.Macro, SourceCodeKind.Statements);

            Dictionary <string, object> importCache = new Dictionary <string, object>(_importCache);

            IsFaulted = false;

            Thread = new Thread(() =>
            {
                Thread = Thread.CurrentThread;

                try
                {
                    StartedEvent?.Invoke();

                    AliasCommands.SetDefaultAliases();

                    ScriptScope macroScope = _engine.CreateScope(importCache);

                    StopWatch.Reset();
                    StopWatch.Start();

                    do
                    {
                        _cancellationToken.Token.ThrowIfCancellationRequested();

                        source.Execute(macroScope);

                        StopWatch.Stop();

                        bool willLoop = _macro.Loop && !IsFaulted && !_cancellationToken.IsCancellationRequested;

                        if (!willLoop)
                        {
                            break;
                        }

                        if (Options.CurrentOptions.Debug)
                        {
                            UO.Commands.SystemMessage(string.Format(Strings.Loop_time___0_, StopWatch.Elapsed));
                        }

                        int diff = 50 - (int)StopWatch.ElapsedMilliseconds;

                        if (diff > 0)
                        {
                            Thread.Sleep(diff);
                        }
                    }while (_macro.Loop && !IsFaulted);
                }
                catch (TaskCanceledException)
                {
                    IsFaulted = true;
                }
                catch (ThreadInterruptedException)
                {
                    IsFaulted = true;
                }
                catch (ThreadAbortException)
                {
                    IsFaulted = true;
                }
                catch (Exception e)
                {
                    IsFaulted = true;
                    Exception = e;

                    ExceptionEvent?.Invoke(e);
                }
                finally
                {
                    StoppedEvent?.Invoke();
                    MacroManager.GetInstance().OnMacroStopped();
                }
            })
            {
                IsBackground = true
            };

            try
            {
                Thread.Start();
                MacroManager.GetInstance().OnMacroStarted();
            }
            catch (ThreadStateException)
            {
                // TODO
            }
            catch (ThreadStartException)
            {
                // TODO
            }
        }