Enqueue() public method

public Enqueue ( ITask task ) : bool
task ITask
return bool
Exemplo n.º 1
0
        public void DispatchEventWithDispatcherQueueExceptionPort()
        {
            using (Dispatcher d = new Dispatcher ()) {
                var dq = new DispatcherQueue ("foo", d);
                var evt = new AutoResetEvent (false);
                var port = new Port<Exception> ();
                dq.UnhandledExceptionPort = port;

                int portPost = 0;
                int dispEx = 0;
                d.UnhandledException += delegate { ++dispEx; };

                var rec = Arbiter.Receive (true, port, (e) => { ++portPost; evt.Set(); });
                rec.TaskQueue = dq;
                rec.Execute ();

                dq.Enqueue (Arbiter.FromHandler (() => { throw new Exception (); }));
                Assert.IsTrue (evt.WaitOne (2000), "#1");
                Assert.AreEqual (1, portPost, "#2");
                Assert.AreEqual (0, dispEx, "#3");

                dq.Enqueue (Arbiter.FromHandler (() => { throw new Exception (); }));
                Assert.IsTrue (evt.WaitOne (2000), "#4");
                Assert.AreEqual (2, portPost, "#5");
                Assert.AreEqual (0, dispEx, "#6");
            }
        }
Exemplo n.º 2
0
 public static void Activate <T>(this DispatcherQueue TaskQueue, params T[] tasks) where T : ITask
 {
     for (int i = 0; i < tasks.Length; i++)
     {
         ITask task = tasks[i];
         TaskQueue.Enqueue(task);
     }
 }
Exemplo n.º 3
0
 public void Cleanup(ITask winner)
 {
     foreach (ReceiverTask current in _concurrentBranches)
     {
         current.Cleanup();
     }
     foreach (ReceiverTask current2 in _mutexBranches)
     {
         current2.Cleanup();
     }
     lock (_mutexBranches)
     {
         CleanupPending(_concurrentBranches);
         CleanupPending(_mutexBranches);
     }
     _dispatcherQueue.Enqueue(winner);
 }
Exemplo n.º 4
0
 public static void ExecuteToCompletion(DispatcherQueue dispatcherQueue, ITask task, Port <EmptyValue> donePort)
 {
     if (task.ArbiterCleanupHandler != null)
     {
         throw new InvalidOperationException(Resource1.TaskAlreadyHasFinalizer);
     }
     task.ArbiterCleanupHandler = delegate
     {
         donePort.Post(EmptyValue.SharedInstance);
     };
     dispatcherQueue.Enqueue(task);
 }
Exemplo n.º 5
0
 public static void Activate(DispatcherQueue dispatcherQueue, params ITask[] arbiter)
 {
     if (dispatcherQueue == null)
     {
         throw new ArgumentNullException("dispatcherQueue");
     }
     if (arbiter == null)
     {
         throw new ArgumentNullException("arbiter");
     }
     for (int i = 0; i < arbiter.Length; i++)
     {
         ITask task = arbiter[i];
         dispatcherQueue.Enqueue(task);
     }
 }
Exemplo n.º 6
0
        public static ITask ExecuteToCompletion(DispatcherQueue dispatcherQueue, ITask task)
        {
            Port <EmptyValue> done = new Port <EmptyValue>();

            if (task.ArbiterCleanupHandler != null)
            {
                throw new InvalidOperationException(Resource1.TaskAlreadyHasFinalizer);
            }
            task.ArbiterCleanupHandler = delegate
            {
                done.Post(EmptyValue.SharedInstance);
            };
            dispatcherQueue.Enqueue(task);
            return(Arbiter.Receive <EmptyValue>(false, done, delegate(EmptyValue e)
            {
            }));
        }
Exemplo n.º 7
0
 public static void Spawn(this DispatcherQueue TaskQueue, Handler handler)
 {
     TaskQueue.Enqueue(new Task(handler));
 }
Exemplo n.º 8
0
 public static void SpawnIterator <T0, T1, T2>(this DispatcherQueue TaskQueue, T0 t0, T1 t1, T2 t2, IteratorHandler <T0, T1, T2> handler)
 {
     TaskQueue.Enqueue(new IterativeTask <T0, T1, T2>(t0, t1, t2, handler));
 }
Exemplo n.º 9
0
 public static void SpawnIterator <T0>(this DispatcherQueue TaskQueue, T0 t0, IteratorHandler <T0> handler)
 {
     TaskQueue.Enqueue(new IterativeTask <T0>(t0, handler));
 }
Exemplo n.º 10
0
 public static void SpawnIterator(this DispatcherQueue TaskQueue, IteratorHandler handler)
 {
     TaskQueue.Enqueue(new IterativeTask(handler));
 }
Exemplo n.º 11
0
 public static void Spawn <T0, T1>(this DispatcherQueue TaskQueue, T0 t0, T1 t1, Handler <T0, T1> handler)
 {
     TaskQueue.Enqueue(new Task <T0, T1>(t0, t1, handler));
 }
Exemplo n.º 12
0
        public void PortSetReceiveToBeUsedWithIterators()
        {
            iterPort = new PortSet (typeof (string), typeof (char), typeof (int));
            iterEvent = new AutoResetEvent (false);
            iterRes = 0;

            using (Dispatcher d = new Dispatcher ()) {
                var disp = new DispatcherQueue ("bla", d);
                disp.Enqueue (new IterativeTask (this.SimpleTaskIterator));
                for (int i = 0; i < 5; ++i)
                    iterPort.PostUnknownType ((i + 1) * 10);
                Assert.IsTrue (iterEvent.WaitOne (2000), "#1");
                Assert.AreEqual (150, iterRes, "#2");
            }
        }
Exemplo n.º 13
0
        public void UnhandledExceptionPort2()
        {
            using (Dispatcher d = new Dispatcher ()) {
                DispatcherQueue dq = new DispatcherQueue ("foo", d);
                var evt = new AutoResetEvent (false);
                int dispEx = 0;
                d.UnhandledException += delegate { ++dispEx; evt.Set (); };

                dq.Enqueue (Arbiter.FromHandler (() => { throw new Exception (); }));

                Assert.IsTrue (evt.WaitOne (2000), "#1");
                Assert.AreEqual (1, dispEx, "#2");
            }
        }
Exemplo n.º 14
0
        public void StuffAfterDipose()
        {
            Dispatcher d = new Dispatcher ();
            var dq = new DispatcherQueue ("foo", d);
            d.Dispose ();
            dq.Dispose ();
            Assert.IsTrue (dq.IsDisposed, "#1");
            try {
                dq.Enqueue (Arbiter.FromHandler( () => { Console.WriteLine ("ff"); }));
                Assert.Fail ("#2");
            } catch (ObjectDisposedException) {}

            d = new Dispatcher (1, ThreadPriority.Normal, DispatcherOptions.SuppressDisposeExceptions, "foo");
            dq = new DispatcherQueue ("foo", d);
            d.Dispose ();
            dq.Dispose ();
            Assert.IsTrue (dq.IsDisposed, "#3");
            Assert.IsFalse (dq.Enqueue (Arbiter.FromHandler( () => {})), "#4");
            Assert.AreEqual (0, dq.ScheduledTaskCount, "#5");
        }
Exemplo n.º 15
0
        internal void PostInternal(bool insertAtHead, PortElement <T> node)
        {
            bool            flag            = false;
            ITask           task            = null;
            DispatcherQueue dispatcherQueue = null;

            lock (Store)
            {
                if (insertAtHead)
                {
                    Store.ElementListAddFirst(node);
                }
                else
                {
                    Store.ElementListAddLast(node);
                }
                if (Store.ReceiverCount == 0)
                {
                    return;
                }
                int num = 1;
                if (Store.ActiveReceiver == null)
                {
                    num = Store.ReceiverCount;
                }
                ReceiverTask receiverTask = Store.ActiveReceiver;
                int          i            = 0;
                while (i < num)
                {
                    if (num != 1)
                    {
                        receiverTask = Store.GetReceiverAtIndex(i);
                    }
                    task            = null;
                    flag            = receiverTask.Evaluate(node, ref task);
                    dispatcherQueue = receiverTask.TaskQueue;
                    if (flag)
                    {
                        Store.ElementListRemove(node);
                        if (receiverTask.State != ReceiverTaskState.Persistent)
                        {
                            Store.RemoveReceiver(receiverTask);
                            if (i > 1)
                            {
                                i--;
                            }
                            else
                            {
                                receiverTask = Store.ActiveReceiver;
                            }
                            num = Store.ReceiverCount;
                            break;
                        }
                        break;
                    }
                    else
                    {
                        if (task != null)
                        {
                            dispatcherQueue.Enqueue(task);
                        }
                        i++;
                    }
                }
            }
            if (flag && task != null)
            {
                dispatcherQueue.Enqueue(task);
            }
        }