Example #1
0
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)

        static int _Run(int mode, string script, string[] args, out string resultS, Action <string> resultA = null)
        {
            var w = WndMsg_; if (w.Is0)
            {
                throw new AuException("Au editor not found.");                                    //CONSIDER: run editor program, if installed
            }
            bool needResult = 0 != (mode & 2); resultS = null;

            using var tr = new _TaskResults();
            if (needResult && !tr.Init())
            {
                throw new AuException("*get task results");
            }

            var data = Util.Serializer_.Serialize(script, args, tr.pipeName);
            int pid  = (int)AWnd.More.CopyDataStruct.SendBytes(w, 100, data, mode);

            switch ((ERunResult)pid)
            {
            case ERunResult.failed: throw new AuException("*start task");

            case ERunResult.notFound: throw new FileNotFoundException($"Script '{script}' not found.");

            case ERunResult.editorThread:
            case ERunResult.deferred: return(0);
            }

            if (0 != (mode & 1))
            {
                using var hProcess = WaitHandle_.FromProcessId(pid, Api.SYNCHRONIZE | Api.PROCESS_QUERY_LIMITED_INFORMATION);
                if (hProcess == null)
                {
                    throw new AuException("*wait for task");
                }

                if (!needResult)
                {
                    hProcess.WaitOne(-1);
                }
                else if (!tr.WaitAndRead(hProcess, resultA))
                {
                    throw new AuException("*get task result");
                }
                else if (resultA == null)
                {
                    resultS = tr.ResultString;
                }

                if (!Api.GetExitCodeProcess(hProcess.SafeWaitHandle.DangerousGetHandle(), out pid))
                {
                    pid = int.MinValue;
                }
            }
            return(pid);
        }
Example #2
0
        //Called from editor's CommandLine. Almost same as _Run. Does not throw.
        internal static int RunCL_(wnd w, int mode, string script, string[] args, Action <string> resultA)
        {
            bool wait = 0 != (mode & 1), needResult = 0 != (mode & 2);

            using var tr = new _TaskResults();
            if (needResult && !tr.Init())
            {
                return((int)RunResult_.cannotGetResult);
            }

            var data = Serializer_.Serialize(script, args, tr.pipeName);
            int pid  = (int)WndCopyData.Send <byte>(w, 101, data, mode);

            if (pid == 0)
            {
                pid--;                       //RunResult_.failed
            }
            switch ((RunResult_)pid)
            {
            case RunResult_.failed:
            case RunResult_.notFound:
                return(pid);

            case RunResult_.deferred:             //possible only if !wait
            case RunResult_.editorThread:         //the script ran sync and already returned. Ignore needResult, as it it auto-detected, not explicitly specified.
                return(0);
            }

            if (wait)
            {
                using var hProcess = WaitHandle_.FromProcessId(pid, Api.SYNCHRONIZE | Api.PROCESS_QUERY_LIMITED_INFORMATION);
                if (hProcess == null)
                {
                    return((int)RunResult_.cannotWait);
                }

                if (!needResult)
                {
                    hProcess.WaitOne(-1);
                }
                else if (!tr.WaitAndRead(hProcess, resultA))
                {
                    return((int)RunResult_.cannotWaitGetResult);
                }

                if (!Api.GetExitCodeProcess(hProcess.SafeWaitHandle.DangerousGetHandle(), out pid))
                {
                    pid = int.MinValue;
                }
            }
            return(pid);
        }
Example #3
0
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)

        static int _Run(int mode, string script, string[] args, out string resultS, Action <string> resultA = null)
        {
            resultS = null;
            var w = WndMsg_; if (w.Is0)
            {
                throw new AuException("Editor process not found.");                                     //CONSIDER: run editor program, if installed
            }
            bool wait = 0 != (mode & 1), needResult = 0 != (mode & 2);

            using var tr = new _TaskResults();
            if (needResult && !tr.Init())
            {
                throw new AuException("*get task results");
            }

            var data = Serializer_.Serialize(script, args, tr.pipeName);
            int pid  = (int)WndCopyData.Send <byte>(w, 100, data, mode);

            if (pid == 0)
            {
                pid--;                       //RunResult_.failed
            }
            switch ((RunResult_)pid)
            {
            case RunResult_.failed:
                return(!wait ? -1 : throw new AuException("*start task"));

            case RunResult_.notFound:
                throw new FileNotFoundException($"Script '{script}' not found.");

            case RunResult_.deferred:             //possible only if !wait
            case RunResult_.editorThread:         //the script ran sync and already returned
                return(0);
            }

            if (wait)
            {
                using var hProcess = WaitHandle_.FromProcessId(pid, Api.SYNCHRONIZE | Api.PROCESS_QUERY_LIMITED_INFORMATION);
                if (hProcess == null)
                {
                    throw new AuException("*wait for task");
                }

                if (!needResult)
                {
                    hProcess.WaitOne(-1);
                }
                else if (!tr.WaitAndRead(hProcess, resultA))
                {
                    throw new AuException("*get task result");
                }
                else if (resultA == null)
                {
                    resultS = tr.ResultString;
                }

                if (!Api.GetExitCodeProcess(hProcess.SafeWaitHandle.DangerousGetHandle(), out pid))
                {
                    pid = int.MinValue;
                }
            }
            return(pid);
        }