Пример #1
0
        public void Invoke(Action action)
        {
            if (_isShuttingDown)
            {
                return;
            }

            var job = new DispatcherJob(action);

            if (_therad.ManagedThreadId == Thread.CurrentThread.ManagedThreadId)
            {
                _queue.Enqueue(job, OperationPriority.Normal);

                ProcessQueue(job);
            }
            else
            {
                ManualResetEvent ev = new ManualResetEvent(false);
                job.Completed += () => ev.Set();

                _queue.Enqueue(job, OperationPriority.Normal);
                if (_isWaiting)
                {
                    _awakeFunction?.Invoke();
                }

                ev.WaitOne();
            }
        }
Пример #2
0
        private void ProcessQueue(DispatcherJob job = null)
        {
            DispatcherJob current = null;

            do
            {
                //TODO:add await for current job
                if (_locker is not null)
                {
                    Wait();
                }
                else if (_queue.TryDequeue(out current))
                {
                    current.Invoke();
                }
                else if (_isShuttingDown)
                {
                    return;
                }
                else if (job is null)
                {
                    Wait();
                }
            }while(!ReferenceEquals(current, job) || job == null);
        }