Exemple #1
0
 public virtual Task Spawn(Executable <Task> exec = null,
                           Handleable postExec    = null)
 {
     lock (mLock) {
         ThrowIfClosed();
         return(new TaskImpl(this, exec, postExec));
     }
 }
Exemple #2
0
 public virtual Task Execute(Executable <Task> exec = null,
                             Handleable postExec    = null)
 {
     lock (mLock) {
         Task task = Spawn(exec, postExec);
         task.Execute();
         return(task);
     }
 }
        public void DoneMoving()
        {
            if (!this.strokes.has(this.ParentId))
            {
                return;
            }

            Handleable parent = (Handleable)this.strokes.get(this.ParentId);

            parent.HandleStoped(this.Id);
        }
        public void Move(StylusPointCollection newPoints)
        {
            if (!this.strokes.has(this.ParentId))
            {
                return;
            }

            Handleable parent = (Handleable)this.strokes.get(this.ParentId);

            parent.handleMoved(this.Id, newPoints[0].ToPoint());
        }
Exemple #5
0
        public virtual Task Next(Executable <Task> exec = null,
                                 Handleable postExec    = null)
        {
            lock (mLock) {
                List <Task> tasks = GetTasksOrThrow();
                if (tasks.Count > 0)
                {
                    Update();
                }

                Task task = new TaskImpl(this, exec, postExec);
                tasks.Add(task);
                return(task);
            }
        }
Exemple #6
0
        protected Request(Executable <Request> exec, Handleable postExec, object lockObj)
        {
            if (lockObj == null)
            {
                lockObj = this;
            }

            mLock     = lockObj;
            mExec     = exec;
            mPostExec = postExec;

            lock (mLock) {
                mState   = STATE_NONE;
                mExcepts = new Exception[0];
            }
        }
Exemple #7
0
 public Request(Executable <Request> exec, Handleable postExec) : this(exec, postExec, null)
 {
 }
Exemple #8
0
        public virtual bool Execute()
        {
            bool skip;

            lock (mLock) {
                int state = mState;
                if ((state
                     & (STATE_RUNNING | STATE_DONE)
                     ) != STATE_READY)
                {
                    return(false);
                }
                state |= STATE_RUNNING;
                mState = state;
                Monitor.PulseAll(mLock);

                skip = (state
                        & (STATE_FAILED | STATE_SUCCESS)
                        & ~STATE_DONE
                        ) != 0;
            }

            Handleable postHandle = null;
            Exception  exception = null;
            bool       success = false, end = false;

            try {
                Executable <Request> exec = mExec;
                if (exec == null && !skip)
                {
                    lock (mLock) {
                        Monitor.Wait(mLock, 20);
                        if ((mState & STATE_DONE) == STATE_DONE)
                        {
                            return(false);
                        }
                        exec       = mExec;
                        postHandle = mPostExec;
                    }
                }
                else
                {
                    postHandle = mPostExec;
                }

                if (!skip)
                {
                    if (exec == null)
                    {
                        throw new NullReferenceException("No executable attached");
                    }

                    exec(this);
                    success = true;
                }
            } catch (Exception ex) {
                exception = ex;
            } finally {
                lock (mLock) {
                    int state = mState;
                    if (exception is ThreadInterruptedException)
                    {
                        state |= STATE_CANCELED;
                    }

                    if ((state & STATE_DONE) == STATE_DONE)
                    {
                        success = false;
                        end     = true;
                    }
                    else if (!skip)
                    {
                        state |= success ? STATE_SUCCESS : STATE_FAILED;
                        AddCause(exception);
                    }

                    state &= ~STATE_RUNNING;
                    state |= STATE_READY | STATE_DONE;
                    mState = state;
                    Monitor.PulseAll(mLock);
                }

                if (postHandle == null)
                {
                    postHandle = mPostExec;
                }
            }
            if (end)
            {
                return(false);
            }

            Handleable postH = postHandle;

            if (postH == null)
            {
                return(success);
            }

            Executable postExec = () => {
                try {
                    int         st;
                    Exception[] ex;
                    lock (mLock) {
                        st = mState;
                        ex = mExcepts;
                    }
                    postH(st, ex);
                } catch (Exception ex) {
                    lock (mLock) {
                        mState |= STATE_POST_FAILED;
                        AddCause(ex);
                        Monitor.PulseAll(mLock);
                    }
                }
            };

            try {
                OnPostExecute(postExec);
            } catch (Exception ex) {
                lock (mLock) {
                    mState |= STATE_POST_FAILED;
                    AddCause(ex);
                    Monitor.PulseAll(mLock);
                }
            }

            return(success);
        }
Exemple #9
0
 public TaskImpl(TaskSpawner spawner, Executable <Task> exec, Handleable postExec)
     : base(exec, postExec, spawner.mLock)
 {
     mSpawner = spawner;
 }
Exemple #10
0
 public TaskImpl(TaskStack stack, Executable <Task> exec, Handleable postExec)
     : base(exec, postExec, stack.mLock)
 {
     mStack = stack;
 }
Exemple #11
0
 public Task(Executable <Task> exec, Handleable postExec) : this(exec, postExec, null)
 {
 }
Exemple #12
0
        public virtual bool Execute()
        {
            Executable taskExec = () => {
                bool skip;
                lock (mLock) {
                    int state = mState;
                    if ((state & STATE_STARTED) != STATE_STARTED)
                    {
                        return;
                    }
                    if ((state & STATE_DONE) == STATE_DONE)
                    {
                        return;
                    }
                    if ((state & STATE_RUNNING) == STATE_RUNNING)
                    {
                        return;
                    }
                    state |= STATE_RUNNING;
                    mState = state;
                    Monitor.PulseAll(mLock);

                    skip = (state
                            & (STATE_FAILED | STATE_SUCCESS)
                            & ~STATE_DONE
                            ) != 0;
                }

                Executable postRun = null;
                Handleable postHandle = null;
                Exception  exception = null;
                bool       success = false, end = false;
                try {
                    Executable <Task> exec = mExec;
                    if (exec == null && !skip)
                    {
                        lock (mLock) {
                            Monitor.Wait(mLock, 20);
                            if ((mState & STATE_DONE) == STATE_DONE)
                            {
                                return;
                            }
                            exec       = mExec;
                            postHandle = mPostExec;
                        }
                    }
                    else
                    {
                        postHandle = mPostExec;
                    }

                    if (!skip)
                    {
                        if (exec == null)
                        {
                            throw new NullReferenceException("No executable attached");
                        }

                        postRun = exec(this);
                        success = true;
                    }
                } catch (Exception ex) {
                    exception = ex;
                } finally {
                    lock (mLock) {
                        int state = mState;
                        if (exception is ThreadInterruptedException)
                        {
                            state |= STATE_CANCELED;
                        }

                        if ((state & STATE_DONE) == STATE_DONE)
                        {
                            success = false;
                            end     = true;
                        }
                        else if (!skip)
                        {
                            state |= success ? STATE_SUCCESS : STATE_FAILED;
                            AddCause(exception);
                        }

                        state &= ~STATE_RUNNING;
                        state |= STATE_DONE;
                        mState = state;
                        Monitor.PulseAll(mLock);
                    }

                    if (postHandle == null)
                    {
                        postHandle = mPostExec;
                    }
                }
                if (end)
                {
                    return;
                }

                Executable postR = success ? postRun : null;
                Handleable postH = postHandle;
                if ((!success || postR == null) &&
                    postH == null)
                {
                    return;
                }

                Executable postExec = () => {
                    if (postR != null)
                    {
                        try {
                            postR();
                        } catch (Exception ex) {
                            lock (mLock) {
                                mState |= STATE_POST_FAILED;
                                AddCause(ex);
                                Monitor.PulseAll(mLock);
                            }
                        }
                    }
                    if (postH != null)
                    {
                        try {
                            int         st;
                            Exception[] ex;
                            lock (mLock) {
                                st = mState;
                                ex = mExcepts;
                            }
                            postH(st, ex);
                        } catch (Exception ex) {
                            lock (mLock) {
                                mState |= STATE_POST_FAILED;
                                AddCause(ex);
                                Monitor.PulseAll(mLock);
                            }
                        }
                    }
                };

                try {
                    if (skip)
                    {
                        postExec();
                    }
                    else
                    {
                        OnPostExecute(postExec);
                    }
                } catch (Exception ex) {
                    lock (mLock) {
                        mState |= STATE_POST_FAILED;
                        AddCause(ex);
                        Monitor.PulseAll(mLock);
                    }
                }
            };

            lock (mLock) {
                int state = mState;
                if ((state & STATE_STARTED) == STATE_STARTED)
                {
                    goto result;
                }
                state    = STATE_STARTED;
                mState   = state;
                mExcepts = null;
                Monitor.PulseAll(mLock);
            }

            try {
                OnExecute(taskExec);
                goto result;
            } catch (Exception ex) {
                lock (mLock) {
                    int state = mState;
                    if ((state & (STATE_DONE | STATE_RUNNING)) == STATE_STARTED)
                    {
                        state |= STATE_FAILED & ~STATE_DONE;
                    }
                    else
                    {
                        goto result;
                    }
                    mState = state;
                    AddCause(ex);
                }
            }
            try {
                OnPostExecute(taskExec);
                goto result;
            } catch (Exception ex) {
                lock (mLock) {
                    int state = mState;
                    if ((state & (STATE_DONE | STATE_RUNNING)) == STATE_STARTED)
                    {
                        state = (STATE_FAILED | STATE_POST_FAILED) & ~STATE_DONE;
                    }
                    else
                    {
                        goto result;
                    }
                    mState = state;
                    AddCause(ex);
                    Monitor.PulseAll(mLock);
                }
            }
            return(false);

result:
            lock (mLock) {
                return((mState & STATE_CANCELED) != STATE_CANCELED);
            }
        }