Ejemplo n.º 1
0
        static public CmdResult DoCmdAct(Command command, Func <CmdResult> task, CmdRequest req, string debugStr, CMDFLAGS flags)
        {
            BotClient robot           = command._mClient;
            string    sync            = command.TaskQueueNameOrNull;
            bool      needResult      = (flags & CMDFLAGS.ForceResult) != 0;
            bool      isConsole       = (flags & CMDFLAGS.IsConsole) != 0;
            bool      forceAsync      = (flags & CMDFLAGS.ForceAsync) != 0;
            bool      forceCompletion = (flags & CMDFLAGS.ForceCompletion) != 0;
            bool      inherit         = (flags & CMDFLAGS.Inherit) != 0;
            bool      scriptMode      = robot != null && robot.InScriptMode;
            bool      cmdRequestsSync = command.ImpliesSync;
            bool      invokeJoin      = scriptMode || cmdRequestsSync;

            if (needResult)
            {
                invokeJoin = true;
            }
            if (forceCompletion)
            {
                forceAsync = false;
            }
            if (forceAsync)
            {
                sync       = null;
                needResult = false;
                invokeJoin = false;
            }
            if (invokeJoin)
            {
                if (!needResult)
                {
                }
                else
                {
                    if (robot != null)
                    {
                        robot.InvokeJoin(debugStr);
                    }
                }
            }
            req.CmdFlags |= flags;
            if (sync != null)
            {
                CmdResult[]      res = new CmdResult[1];
                ManualResetEvent mre = new ManualResetEvent(false);
                Abortable        tq  = null;
                if (robot == null)
                {
                    tq = Cogbot.ClientManager.OneAtATimeQueue;
                }
                else
                {
                    tq = robot.GetTaskQueueHandler(sync, true);
                }
                tq.Enqueue(() =>
                {
                    try
                    {
                        res[0] = task();
                    }
                    finally
                    {
                        if (needResult)
                        {
                            mre.Set();
                        }
                    }
                });
                if (needResult)
                {
                    mre.WaitOne();
                }
                return(res[0]);
            }
            else
            {
                return(task());
            }
            //robot.OneAtATimeQueue.Enqueue(cmdStr, () => command.acceptInputWrapper(verb, args, callerID, del));
        }
Ejemplo n.º 2
0
        public string CreateTask(string id, ThreadStart task, string debugName0, bool createFresh, bool kill, EventWaitHandle mre, OutputDelegate WriteLine)
        {
            BotClient TheBotClient = this;

            string[]    debugName = new[] { debugName0 };
            ThreadStart thread    =
                () =>
            {
                try
                {
                    try
                    {
                        task();
                    }
                    catch (ThreadAbortException e)
                    {
                        WriteLine("Aborting " + debugName[0]);
                    }
                    catch (Exception e)
                    {
                        WriteLine("Problem with {0} {1}", debugName[0], e);
                    }
                }
                finally
                {
                    try
                    {
                        if (mre != null)
                        {
                            mre.Set();
                        }
                        if (createFresh)
                        {
                            TheBotClient.RemoveThread(Thread.CurrentThread);
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                    }
                    catch (StackOverflowException)
                    {
                    }
                    catch (Exception)
                    {
                    }
                    WriteLine("done with " + debugName[0]);
                }
            };
            String threadName = id;

            if (createFresh)
            {
                TheBotClient.InvokeThread(threadName, thread);
            }
            else
            {
                Abortable tq = TheBotClient.GetTaskQueueHandler(id, true);
                if (kill)
                {
                    tq.Abort();
                    tq.Resume();
                }
                if (task != null)
                {
                    tq.Enqueue(thread);
                }
                debugName[0] += tq;
            }
            return(debugName[0]);
        }