Exemplo n.º 1
0
        internal Task <T> Send <T>(DispatchPriority priority, Func <T> operation)
        {
            var task = new Task <T>(operation);

            Post(priority, () => task.RunSynchronously());
            return(task);
        }
Exemplo n.º 2
0
 public void Enqueue(DispatchPriority priority, Action operation)
 {
     lock (_lock)
     {
         _queuedOperations[(int)priority].Enqueue(operation);
     }
 }
Exemplo n.º 3
0
        internal Task Send(DispatchPriority priority, Action operation)
        {
            var task = new Task(operation);

            Post(priority, () => task.RunSynchronously());
            return(task);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Adds the <paramref name="operation"/> to the operation queue and stops the current thread until it's executed.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 /// <returns>Result value of the performed operation</returns>
 /// <remarks>If called from within an operation currently executed by the <see cref="Current"/>, the provided <paramref name="operation"/> will be performed in place to avoid deadlocks.</remarks>
 public T Invoke <T>(DispatchPriority priority, Func <T> operation)
 {
     if (CheckAccess())
     {
         return(operation());
     }
     else
     {
         return(InvokeAsync(priority, operation).Result);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Adds the <paramref name="operation"/> to the operation queue and stops the current thread until it's executed.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 /// <remarks>If called from within an operation currently executed by the <see cref="Current"/>, the provided <paramref name="operation"/> will be performed in place to avoid deadlocks.</remarks>
 public void Invoke(DispatchPriority priority, Action operation)
 {
     if (CheckAccess())
     {
         operation();
     }
     else
     {
         _synchronizationContext.Send(priority, operation);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// If called for another thread, works similarly to the <seealso cref="Dispatch(DispatchPriority, Func{Task})"/>, but if called from the <see cref="Current"/> thread - executes the <paramref name="operation"/> inline.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public void Run(DispatchPriority priority, Func <Task> operation)
 {
     if (CheckAccess())
     {
         operation();
     }
     else
     {
         Dispatch(priority, operation);
     }
 }
Exemplo n.º 7
0
        public int Count(DispatchPriority minPriority)
        {
            int count = 0;

            lock (_lock)
            {
                for (var priority = DispatchPriority.High; priority >= minPriority; --priority)
                {
                    count += _queuedOperations[(int)priority].Count;
                }
            }

            return(count);
        }
Exemplo n.º 8
0
        internal static void BeginInvoke(this FrameworkElement fe, Action action, DispatchPriority dispatcherPriority)
        {
            if (fe == null)
            {
                throw new ArgumentNullException("fe");
            }

            var dispatcherInstance = fe.Dispatcher;

            if (dispatcherInstance == null)
            {
                action();
                return;
            }

#if !SILVERLIGHT
            dispatcherInstance.BeginInvoke((DispatcherPriority)dispatcherPriority, action);
#else
            dispatcherInstance.BeginInvoke(action);
#endif
        }
 private void Dispatch(DispatchPriority priority, Action action) => _dispatcher.Dispatch(priority, action);
Exemplo n.º 10
0
 internal void Post(DispatchPriority priority, Action operation)
 {
     _operationQueue.Enqueue(priority, operation);
     _waitToken?.Cancel();
 }
Exemplo n.º 11
0
 public bool Any(DispatchPriority minPriority)
 {
     return(Count(minPriority) > 0);
 }
 private void AssertNone(DispatchPriority minPriority = DispatchPriority.Low)
 {
     Assert.IsFalse(_queue.Any(minPriority));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Adds the <paramref name="operation"/> to the operation queue and returns a <see cref="Task"/> associated with the state of its execution.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public Task InvokeAsync(DispatchPriority priority, Action operation)
 {
     return(_synchronizationContext.Send(priority, operation));
 }
 private void AssertCount(int count, DispatchPriority minPriority = DispatchPriority.Low)
 {
     Assert.AreEqual(count, _queue.Count(minPriority));
 }
 private void AssertAny(DispatchPriority minPriority = DispatchPriority.Low)
 {
     Assert.IsTrue(_queue.Any(minPriority));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Adds the asynchronous <paramref name="operation"/> to the operation queue and returns a <see cref="Task"/> associated with the state of its execution.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public async Task InvokeAsync(DispatchPriority priority, Func <Task> operation)
 {
     await await InvokeAsync <Task>(priority, operation);
 }
 private void Enqueue(DispatchPriority priority, int value = 0)
 {
     _queue.Enqueue(priority, () => _dequeuedValue = value);
 }
 private void Invoke(DispatchPriority priority, Action action) => _dispatcher.Invoke(priority, action);
 private void Enqueue(TimeSpan timeSpan, DispatchPriority priority, Action action)
 {
     _queue.Enqueue(_now.Add(timeSpan), priority, action);
 }
 private void Dispatch(DispatchPriority priority, Func <Task> action) => _dispatcher.Dispatch(priority, action);
Exemplo n.º 21
0
 /// <summary>
 /// Adds the asynchronous <paramref name="operation"/> to the operation queue without blocking of the current thread (fire and forget).
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public void Dispatch(DispatchPriority priority, Func <Task> operation)
 {
     _synchronizationContext.Post(priority, () => operation());
 }
Exemplo n.º 22
0
 /// <summary>
 /// Adds the <paramref name="operation"/> to the operation queue without blocking of the current thread (fire and forget).
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public void Dispatch(DispatchPriority priority, Action operation)
 {
     _synchronizationContext.Post(priority, operation);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Adds the asynchronous <paramref name="operation"/> to the operation queue and returns a <see cref="Task"/> associated with the state of its execution.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public async Task <T> InvokeAsync <T>(DispatchPriority priority, Func <Task <T> > operation)
 {
     return(await await InvokeAsync <Task <T> >(priority, operation));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Adds the <paramref name="operation"/> to the operation queue and returns a <see cref="Task"/> associated with the state of its execution.
 /// </summary>
 /// <param name="priority">Priority of the operation</param>
 /// <param name="operation">Operation to be queued</param>
 public Task <T> InvokeAsync <T>(DispatchPriority priority, Func <T> operation)
 {
     return(_synchronizationContext.Send(priority, operation));
 }
 private void Schedule(TimeSpan timeSpan, DispatchPriority priority, Action action) => _dispatcher.Schedule(timeSpan, priority, action);
Exemplo n.º 26
0
 /// <summary>
 /// Schedules the execution of the <paramref name="operation"/> after the provided <paramref name="delay"/>.
 /// </summary>
 /// <param name="delay"></param>
 /// <param name="operation">Operation to be scheduled</param>
 /// <remarks>
 /// It is not guaranteed that the <paramref name="operation"/> will be executed exactly after the provided <paramref name="delay"/> (it's only guaranteed that it will be queued not sooner then that).
 /// </remarks>
 public void Schedule(TimeSpan delay, DispatchPriority priority, Action operation)
 {
     _synchronizationContext.PostDelayed(DateTime.UtcNow + delay, priority, operation);
 }
 private T Invoke <T>(DispatchPriority priority, Func <T> func) => _dispatcher.Invoke(priority, func);
Exemplo n.º 28
0
 /// <summary>
 /// Schedules the execution of the <paramref name="operation"/> after the provided <paramref name="delay"/>.
 /// </summary>
 /// <param name="delay"></param>
 /// <param name="operation">Operation to be scheduled</param>
 /// <remarks>
 /// It is not guaranteed that the <paramref name="operation"/> will be executed exactly after the provided <paramref name="delay"/> (it's only guaranteed that it will be queued not sooner then that).
 /// </remarks>
 public void Schedule(TimeSpan delay, DispatchPriority priority, Func <Task> operation)
 {
     _synchronizationContext.PostDelayed(DateTime.UtcNow + delay, priority, () => operation());
 }
 private void Enqueue(TimeSpan timeSpan, DispatchPriority priority, int value = 0)
 {
     _queue.Enqueue(_now.Add(timeSpan), priority, () => _dequeuedValues.Add(value));
 }
Exemplo n.º 30
0
 /// <summary>
 /// When awaited, will yield the execution of the current dispatcher to other queued operations with at least given <paramref name="priority"/>.
 /// </summary>
 /// <param name="priority">The priority of operations to be executed</param>
 /// <exception cref="DispatcherException">Throws the <see cref="DispatcherException"/> if no dispatcher is currently running on the calling thread.</exception>
 public static YieldTask Yield(DispatchPriority priority = DispatchPriority.Low)
 {
     return(new YieldTask(priority));
 }