Exemple #1
0
        //

        /// <summary>
        /// 向此界面消息处理器推送一条消息。
        /// </summary>
        /// <param name="message">消息。</param>
        public void PushMessage(UIMessage message)
        {
            if (message is null)
            {
                throw new ArgumentNullException();
            }

            if (State != UIMessageProcessorState.Running)
            {
                throw new InvalidOperationException();
            }

            //

            if (!message.TryUpdateState(UIMessageState.WaitingToProcess))
            {
                throw new InvalidOperationException();
            }

            _MessageQueueLock.EnterWriteLock();

            try
            {
                Queue <UIMessage> messageQueue = (message.AllowAsync ? _AsyncMessages : _SyncMessages);

                messageQueue.Enqueue(message);
            }
            finally
            {
                _MessageQueueLock.ExitWriteLock();
            }
        }
Exemple #2
0
        /// <summary>
        /// 执行一次消息循环。
        /// </summary>
        protected virtual void MessageLoop()
        {
            if (State == UIMessageProcessorState.Running)
            {
                int asyncMessageCount;

                _MessageQueueLock.EnterReadLock();

                try
                {
                    asyncMessageCount = _AsyncMessages.Count;
                }
                finally
                {
                    _MessageQueueLock.ExitReadLock();
                }

                if (asyncMessageCount > 0)
                {
                    int            processAsyncMessageCount;
                    HashSet <long> discardAsyncMessageUids;

                    _MessageQueueLock.EnterWriteLock();

                    try
                    {
                        SelectAsyncMessagesForThisLoop(_AsyncMessages, out processAsyncMessageCount, out discardAsyncMessageUids);
                    }
                    finally
                    {
                        _MessageQueueLock.ExitWriteLock();
                    }

                    if (processAsyncMessageCount < 0)
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    else if (processAsyncMessageCount > 0)
                    {
                        if (processAsyncMessageCount > asyncMessageCount)
                        {
                            processAsyncMessageCount = asyncMessageCount;
                        }

                        //

                        List <UIMessage> asyncMessages = new List <UIMessage>(processAsyncMessageCount);

                        _MessageQueueLock.EnterWriteLock();

                        try
                        {
                            for (int i = 0; i < processAsyncMessageCount; i++)
                            {
                                asyncMessages.Add(_AsyncMessages.Dequeue());
                            }
                        }
                        finally
                        {
                            _MessageQueueLock.ExitWriteLock();
                        }

                        for (int i = 0; i < processAsyncMessageCount; i++)
                        {
                            if (State == UIMessageProcessorState.Stopping)
                            {
                                return;
                            }

                            UIMessage message = asyncMessages[i];

                            if (message.AllowDiscard && !(discardAsyncMessageUids is null) && discardAsyncMessageUids.Contains(message.Uid))
                            {
                                message.TryUpdateState(UIMessageState.Discarded);

                                message.TryReply();
                            }
                            else
                            {
                                Task.Run(() =>
                                {
                                    _IncreaseProcessingAsyncMessageCount();

                                    //

                                    message.TryUpdateState(UIMessageState.Processing);

                                    try
                                    {
                                        ProcessMessage(message);

                                        message.TryUpdateState(UIMessageState.ProcessCompleted);
                                    }
                                    catch (Exception ex)
                                    {
                                        message.Exception = ex;

                                        message.TryUpdateState(UIMessageState.ProcessFailed);
                                    }

                                    message.TryReply();

                                    //

                                    _DecreaseProcessingAsyncMessageCount();
                                });
                            }
                        }
                    }
                }
            }

            if (State == UIMessageProcessorState.Running)
            {
                int syncMessageCount;

                _MessageQueueLock.EnterReadLock();

                try
                {
                    syncMessageCount = _SyncMessages.Count;
                }
                finally
                {
                    _MessageQueueLock.ExitReadLock();
                }

                if (syncMessageCount > 0)
                {
                    int            processSyncMessageCount;
                    HashSet <long> discardSyncMessageUids;

                    _MessageQueueLock.EnterWriteLock();

                    try
                    {
                        SelectSyncMessagesForThisLoop(_SyncMessages, out processSyncMessageCount, out discardSyncMessageUids);
                    }
                    finally
                    {
                        _MessageQueueLock.ExitWriteLock();
                    }

                    if (processSyncMessageCount < 0)
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    else if (processSyncMessageCount > 0)
                    {
                        if (processSyncMessageCount > syncMessageCount)
                        {
                            processSyncMessageCount = syncMessageCount;
                        }

                        //

                        List <UIMessage> syncMessages = new List <UIMessage>(processSyncMessageCount);

                        _MessageQueueLock.EnterWriteLock();

                        try
                        {
                            for (int i = 0; i < processSyncMessageCount; i++)
                            {
                                syncMessages.Add(_SyncMessages.Dequeue());
                            }
                        }
                        finally
                        {
                            _MessageQueueLock.ExitWriteLock();
                        }

                        for (int i = 0; i < processSyncMessageCount; i++)
                        {
                            if (State == UIMessageProcessorState.Stopping)
                            {
                                return;
                            }

                            UIMessage message = syncMessages[i];

                            if (message.AllowDiscard && !(discardSyncMessageUids is null) && discardSyncMessageUids.Contains(message.Uid))
                            {
                                message.TryUpdateState(UIMessageState.Discarded);

                                message.TryReply();
                            }
                            else
                            {
                                message.TryUpdateState(UIMessageState.Processing);

                                try
                                {
                                    ProcessMessage(message);

                                    message.TryUpdateState(UIMessageState.ProcessCompleted);
                                }
                                catch (Exception ex)
                                {
                                    message.Exception = ex;

                                    message.TryUpdateState(UIMessageState.ProcessFailed);
                                }

                                message.TryReply();
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// 处理一个消息。
 /// </summary>
 /// <param name="message">消息。</param>
 protected virtual void ProcessMessage(UIMessage message)
 {
     throw new NotImplementedException();
 }