コード例 #1
0
        public async Task ExecuteScript(Hotkey hotkey, CancellationToken token)
        {
            using var threadInputScope = AttachedThreadInputScope.Create();
            using var waitCursor       = WaitCursorScope.Create();

            var scriptRunners = _scriptRunners.Value;

            if (!scriptRunners.TryGetValue(hotkey.Name, out var scriptRunner))
            {
                var code = hotkey.Data !.ToString();
                scriptRunner = await Task.Run(() =>
                {
                    var script = CSharpScript.Create <object>(
                        code: code,
                        options: ScriptOptions.Value,
                        globalsType: typeof(IScriptGlobals));

                    script.Compile(token);
                    return(script.CreateDelegate());
                }, token);

                scriptRunners.Add(hotkey.Name, scriptRunner);
            }

            var globals = new ScriptGlobals(this.Host, token);
            var result  = await scriptRunner(globals, token);

            if (result is Task task)
            {
                await task;
            }

            Host.PlayNotificationSound();
        }