Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to lock the gate. The action is run once the gate becomes free.
        /// Queued actions are run in order.
        /// </summary>
        public virtual void TryLock(IAction onAvailable, Needle needle = null, bool autoUnlock = true)
        {
            // replace action to unlock once task has run
            if (autoUnlock)
            {
                onAvailable = new ActionPair(onAvailable, Release);
            }

            // try get the lock
            if (TryTake)
            {
                // replace needle reference if needed
                if (needle == null)
                {
                    ManagerUpdate.Control.AddSingle(onAvailable);
                }
                else
                {
                    needle.AddSingle(onAvailable);
                }
                return;
            }

            // add to queue
            _queue.Enqueue(new Act(onAvailable));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to lock the gate. The action is run once the gate becomes free.
        /// Queued actions are run in order.
        /// </summary>
        public override void TryLock(IAction onAvailable, Needle needle = null, bool autoUnlock = true)
        {
            // try get the lock
            if (TryTake)
            {
                // run the on lock method if set
                if (OnLock != null)
                {
                    OnLock.Run();
                }
                if (needle == null)
                {
                    needle = ManagerUpdate.Control;
                }

                // replace action to unlock once task has run
                if (autoUnlock)
                {
                    needle.AddSingle(new ActionPair(onAvailable, Release));
                }
                else
                {
                    // run on lock function
                    needle.AddSingle(onAvailable);
                }

                return;
            }

            if (autoUnlock)
            {
                // set an action to unlock once task has run and add the task to the queue
                _queue.Enqueue(new Act(new ActionPair(onAvailable, Release), needle));
            }
            else
            {
                // add the on available task
                _queue.Enqueue(new Act(onAvailable, needle));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Method that receives the send delegate and executes it on a thread synchronously.
        /// </summary>
        public override void Send(SendOrPostCallback d, object state)
        {
            // set the current context
            //SynchronizationContext.SetSynchronizationContext(_needle.Context);

            // create an action that will be run safely on the needle
            ActionTryCatch action = new ActionTryCatch(ActionSet.New(Run, d, state));

            // have the delegate run on the needle and wait for it to complete
            _needle.AddSingle(action);

            // wait while the action has not run
            while (!action.Ran)
            {
                Thread.Sleep(1);
            }

            // the action is complete - was there an exception?
            if (action.Exception != null)
            {
                // yes, rethrow
                throw action.Exception;
            }
        }