protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {            
            var inputParams = Parameters ?? new List<InArgument<string>>();

            if (inputParams.Count > 10)
                throw new ArgumentException("Only 10 parameters are allowed.");

            var fileInfo = new FileInfo(ScriptPath.Get(context));
            var fullName = fileInfo.FullName;
            var functionName = FunctionName.Get(context);

            if (!fileInfo.Exists)
                throw new ArgumentException($"'{fullName}' does not exist.");

            if (string.Compare(fileInfo.Extension, ".ahk", true) != 0)
                throw new ArgumentException($"'{fileInfo.Extension}' is not a valid AutoHotKey file type.");

            var paramList = inputParams.Select(x => x.Get(context)).ToList();

            var tcs = new TaskCompletionSource<string>(state);
            var cts = new CancellationTokenSource();
            context.UserState = cts;

            var task = Task.Factory.StartNew(() => {
                Engine = new AutoHotkeyExecutor();
                if (string.IsNullOrWhiteSpace(functionName))
                {
                    return Engine.ExecuteRaw(fileInfo);
                }
                else
                {
                    Engine.Load(fullName);

                    if (!Engine.FunctionExists(functionName))
                        throw new ArgumentException($"The function '{functionName}' does not exist in the provided file: '{fullName}'.");

                    return Engine.ExecuteFunction(functionName, paramList.ToArray());
                }
            }, cts.Token);

            task.ContinueWith(t => {
                if (t.IsFaulted) tcs.TrySetException(t.Exception.InnerExceptions);
                else if (t.IsCanceled) tcs.TrySetCanceled();
                if (callback != null) callback(tcs.Task);
                tcs.TrySetResult(t.Result);
            });

            return tcs.Task;
        }
Beispiel #2
0
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            var scriptPath = ScriptPath.Get(context);

            if (Path.GetExtension(scriptPath) != ".ps1")
            {
                throw new ArgumentException($"'{Path.GetExtension(scriptPath)}' is not a valid PowerShell file type.");
            }

            var parameters = Parameters.Select(x => new KeyValuePair <string, object>(x.Key, x.Value.Get(context))).ToList();
            var psExec     = new PowerShellExecutor();

            context.UserState = psExec;
            return(psExec.ExecuteScript(scriptPath, parameters, callback, state));
        }