首页 > 技术文章 > 接收队列信息

6B23 2020-01-17 15:28 原文

        /// <summary>
        /// 接收队列信息
        /// </summary>
        /// <returns></returns>
        private bool receiveSimpleMsg()
        {
            try
            {
                MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
                //string path = System.Configuration.ConfigurationSettings.AppSettings.Get("ReceiveMsgQueue");
                //MessageQueue MQ = new MessageQueue(path);
                //调用MessageQueue的Receive方法接收消息  
                if (MQ.GetAllMessages().Length > 0)
                {
                    System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                        strGNo = message.Label;
                        strNumber = message.Body.ToString().Split(';')[0];
                        strPatientName = message.Body.ToString().Split(';')[1];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                LogCommon.WriteLog("读取队列产生错误" +ex.Source.ToString() + "-" + ex.TargetSite.Name + "-" + ex.Message);
            }
            return false;
        }

推荐阅读