void ThreadProc( ) { while (true) { Rock.Mobile.Util.Debug.WriteLine("ThreadProc: Sleeping..."); QueueProcessHandle.WaitOne( ); Rock.Mobile.Util.Debug.WriteLine("ThreadProc: Waking for work"); // while there are requests pending, process them while (RequestQueue.IsEmpty == false) { Rock.Mobile.Util.Debug.WriteLine("ThreadProc: Processing Request"); // get the web request out of the queue RequestUpdateHandle.WaitOne( ); //Wait to make sure no other thread is using the Queue IWebRequestObject requestObj = null; RequestQueue.TryDequeue(out requestObj); //yank it out RequestUpdateHandle.Set( ); // all clear if (requestObj != null) { // execute it requestObj.ProcessRequest( ); } } } }
/// <summary> /// The one entry point, this is where requests should be sent /// </summary> public void TryPushRequest(IWebRequestObject requestObj, IResultHandler resultHandler) { // first, lock the queue RequestUpdateHandle.WaitOne( ); // now we can be sure that it won't be pulled out and processed while we're trying to attach a handler. IWebRequestObject currObj = RequestQueue.Where(r => r.GetRequestUrl( ) == requestObj.GetRequestUrl( )).SingleOrDefault( ); if (currObj != null) { Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} already requested. Not queueing.", currObj.GetRequestUrl( ))); currObj.AttachResultHandler(resultHandler); } else { RequestQueue.Enqueue(requestObj); Rock.Mobile.Util.Debug.WriteLine("Setting Wait Handle"); QueueProcessHandle.Set( ); } // notify the thread it's ok to pull out more queue objects RequestUpdateHandle.Set( ); }