Пример #1
0
        /// <summary>Initiates an asynchronous receive operation that has a specified time-out and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.</summary>
        /// <param name="callback">The <see cref="T:System.AsyncCallback"></see> that receives the notification of the asynchronous operation completion. </param>
        /// <param name="state">A state object, specified by the application, that contains information associated with the asynchronous operation. </param>
        /// <param name="timeout">A <see cref="T:System.TimeSpan"></see> that indicates the interval of time to wait for a message to become available. </param>
        /// <returns>The <see cref="T:System.IAsyncResult"></see> that identifies the posted asynchronous request.</returns>
        public IAsyncResult BeginReceive(TimeSpan timeout, object state, AsyncCallback callback)
        {
            long totalMilliseconds = (long)timeout.TotalMilliseconds;

            if ((totalMilliseconds < 0L) || (totalMilliseconds > 4294967295L))
            {
                throw new ArgumentException("InvalidParameter", "timeout");
            }
            ReceiveItemCallback caller = new ReceiveItemCallback(ReceiveItemWorker);

            if (callback == null)
            {
                callback = CreateCallBack();
            }
            if (state == null)
            {
                state = new object();
            }
            // Initiate the asychronous call.  Include an AsyncCallback
            // delegate representing the callback method, and the data
            // needed to call EndInvoke.
            IAsyncResult result = caller.BeginInvoke(timeout, state, callback, caller);

            this.resetEvent.Set();
            return(result);
        }
Пример #2
0
        /// <summary>Completes the specified asynchronous receive operation.</summary>
        /// <param name="asyncResult"></param>
        /// <returns></returns>
        public IQueueItem EndReceive(IAsyncResult asyncResult)
        {
            // Retrieve the delegate.
            ReceiveItemCallback caller = (ReceiveItemCallback)asyncResult.AsyncState;

            // Call EndInvoke to retrieve the results.
            IQueueItem item = (IQueueItem)caller.EndInvoke(asyncResult);

            AsyncCompleted(item);
            this.resetEvent.WaitOne();
            return(item);
        }
Пример #3
0
        /// <summary>
        /// AsyncReceive
        /// </summary>
        /// <returns></returns>
        public IQueueItem AsyncReceive()
        {
            object   state = new object();
            TimeSpan ts    = TimeSpan.FromSeconds(QueueDefaults.DefaultRecieveTimeOutInSecond);

            ReceiveItemCallback caller = new ReceiveItemCallback(ReceiveItemWorker);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(ts, state, CreateCallBack(), caller);

            Thread.Sleep(10);

            result.AsyncWaitHandle.WaitOne();

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            IQueueItem item = caller.EndInvoke(result);

            AsyncCompleted(item);
            return(item);
        }