Example #1
0
        public void InvokeResultTest()
        {
            DispatcherOperation operation0 = new DispatcherOperation(() => "operation-result", DispatcherPriority.Normal);

            operation0.Invoke();
            Assert.AreEqual("operation-result", operation0.Result);
        }
Example #2
0
        public void InvokeParametersTest()
        {
            int value = 0;

            DispatcherOperation operation0 = new DispatcherOperation(() => value = 1, DispatcherPriority.Normal);

            operation0.Invoke();
            Assert.AreEqual(1, value);
        }
Example #3
0
 /// <summary>
 /// Executes the specified delegate synchronously on the thread the Dispatcher is associated with.
 /// </summary>
 public void Invoke(Delegate d, object args)
 {
     if (CheckAccess())
     {
         DispatcherOperation.Invoke(d, args);
     }
     else
     {
         DispatcherOperation operation = AddOperation(d, args, new AutoResetEvent(false));
         operation.Wait();
     }
 }
Example #4
0
        private void AddOperation(DispatcherPriority priority, Delegate d, object args, AutoResetEvent wait = null)
        {
            if (priority == DispatcherPriority.Invalid ||
                priority == DispatcherPriority.Inactive)
            {
                throw new ArgumentException("Invalid Priority");
            }

            if (d == null)
            {
                throw new ArgumentNullException("Null method");
            }

            if (priority == DispatcherPriority.Send && CheckAccess())
            {
                // Fast path: invoking at Send priority
                DispatcherOperation.Invoke(d, args, _context);
            }
            else
            {
                // Slow path: going through the queue
                DispatcherOperation operation = new DispatcherOperation
                {
                    Priority  = priority,
                    Callback  = d,
                    Args      = args,
                    WaitEvent = wait
                };

                lock (_operations)
                {
                    _operations.Enqueue(operation);
                }

                if (wait != null)
                {
                    if (CheckAccess())
                    {
                        // We cannot lock this thread, so process the queue now
                        ProcessQueue();
                    }
                    else
                    {
                        // Block this thread and wait for the operation to get finished
                        operation.Wait();
                    }
                }
            }
        }