Пример #1
0
        /// <summary>
        /// 发送消息,并根据消息中的超时时间等待数据返回
        /// </summary>
        /// <param name="ieCommandMessage"></param>
        /// <returns></returns>
        public IeCommandMessage SendAndGet(IeCommandMessage ieCommandMessage)
        {
            try
            {
                if (!Send(ieCommandMessage, out var key))
                {
                    return(null);
                }

                var startTime = DateTime.Now;

                do
                {
                    var messageObj = GetReturnValue(key);

                    if (messageObj != null)
                    {
                        return(messageObj);
                    }
                } while (DateTime.Now - startTime < TimeSpan.FromMilliseconds(ieCommandMessage.TimeoutMillisecond));

                throw new TimeoutException();
            }
            catch (Exception e)
            {
                //_cancellationTokenSource.Cancel();
                throw e;
            }
        }
Пример #2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="ieCommandMessage"></param>
        /// <param name="returnDataDictionaryKey"></param>
        /// <returns></returns>
        private bool Send(IeCommandMessage ieCommandMessage, out string returnDataDictionaryKey)
        {
            returnDataDictionaryKey = ieCommandMessage.ReturnDataDictionaryKey = $"BrowserCommand{ieCommandMessage.ManagedThreadId.ToString().PadLeft(3, '0')}{(++_sharedMemoryId).ToString().PadLeft(6, '0')}";

            if (!_returnData.TryAdd(ieCommandMessage.ReturnDataDictionaryKey, null))
            {
                return(false);
            }

            _processQueue.Enqueue(ieCommandMessage);
            return(true);
        }
Пример #3
0
        /// <summary>
        /// 该方法对入队的每个元素进行处理
        /// </summary>
        /// <param name="value"></param>
        private void ProcessQueue_ProcessItemEvent(IeCommandMessage value)
        {
            Task.Factory.StartNew(() =>
            {
                do
                {
                    value.ResultMessage = new ResultMessage
                    {
                        ReturnParameters = new
                        {
                            RunningId = ++_runningId
                        }
                    };

                    if (_returnData.TryGetValue(value.ReturnDataDictionaryKey, out var resultIeCommand) && resultIeCommand == null)
                    {
                        _returnData[value.ReturnDataDictionaryKey] = value;
                    }

                    if (_runningId == 50)
                    {
                        Thread.Sleep(value.TimeoutMillisecond + 1000);
                    }

                    Thread.Sleep(20);

                    value.IsTimeout  = false;
                    value.ResultTime = DateTime.Now;
                } while (value.IsTimeout);
            }).Wait(value.TimeoutMillisecond);

            if (value.IsTimeout)
            {
                throw new TimeoutException();
            }
        }
Пример #4
0
        private static void Demo()
        {
            for (var i = 0; i < 100; i++)
            {
                var ieCommandManage = new IeCommandMessage
                {
                    MethodName     = "demo",
                    CommandMessage = new CommandMessage()
                    {
                        CustomId = "demoCustomId"
                    }
                };

                try
                {
                    var returnIe = Singleton.Instance.SendAndGet(ieCommandManage);
                    Console.WriteLine($"after send : {JsonConvert.SerializeObject(returnIe?.ResultMessage)}");
                }
                catch (Exception e)
                {
                    break;
                }
            }
        }
Пример #5
0
 /// <summary>
 ///  处理异常
 /// </summary>
 /// <param name="obj">队列实例</param>
 /// <param name="ex">异常对象</param>
 /// <param name="value">出错的数据</param>
 private void ProcessQueue_ProcessExceptionEvent(ProcessQueue <IeCommandMessage> obj, Exception ex, IeCommandMessage value)
 {
     Console.WriteLine($"ProcessQueue_ProcessExceptionEvent -> before stop {obj.GetInternalItemCount()}");
     new Task(obj.StopAndClear).Start();
     Console.WriteLine($"ProcessQueue_ProcessExceptionEvent -> after stop {obj.GetInternalItemCount()}");
 }