/// <summary> /// Useful to post intermediate callbacks from an async work /// </summary> /// <param name="request">The async work for which this intermediate call is being made.</param> /// <param name="callback">The user callback to be invoked</param> /// <param name="state">User passed state param to the callback</param> public void PostProgress(AsyncWorkRequest request, SendOrPostCallback callback, object state) { if (request.AsyncOperation == null) { throw new AsyncWorkManagerException("Cannot post progress for a worker WebRequest that hasnt been started yet."); } request.AsyncOperation.Post(callback, state); }
// Reset the state of the objects void ResetAsyncWorkerManager() { lock (this._lockObject) { this._asyncWorkManager = null; this._refreshRequestWorker = null; this._cacheRequestHandler = null; this._controllerBehavior.Locked = false; this._beginSessionComplete = false; } }
/// <summary> /// Called when the associated AsyncWorkRequest is to be completed. /// </summary> /// <param name="workRequest">The AsyncWorkRequest that is to be completed.</param> /// <param name="completionArguments">Completion arguments</param> public void CompleteWorkRequest(AsyncWorkRequest workRequest, object completionArguments) { if (workRequest.AsyncOperation == null) { throw new AsyncWorkManagerException("Cannot complete a worker WebRequest that hasnt been started yet."); } // Post the completion callback workRequest.AsyncOperation.PostOperationCompleted(new SendOrPostCallback(workRequest.CompletionCallback), completionArguments); // Process the next request this.ProcessNextWorkRequest(); }
void ProcessCacheRequestWorker(AsyncWorkRequest worker, object[] inputParams) { CacheRequest request = inputParams [0] as CacheRequest; object state = inputParams [1]; _wrapper = new AsyncArgsWrapper() { UserPassedState = state, WorkerRequest = worker, CacheRequest = request }; ProcessRequest(); }
/// <summary> /// Actual worker performing the work /// </summary> /// <param name="worker">AsyncWorkRequest object</param> /// <param name="inputParams">input parameters</param> void ProcessCacheRequestWorker(AsyncWorkRequest worker, object[] inputParams) { Debug.Assert(inputParams.Length == 2); CacheRequest request = inputParams[0] as CacheRequest; object state = inputParams[1]; AsyncArgsWrapper wrapper = new AsyncArgsWrapper() { UserPassedState = state, WorkerRequest = worker, CacheRequest = request }; ProcessRequest(wrapper); }
/// <summary> /// Called when the associated AsyncWorkRequest is to be completed. /// </summary> /// <param name="workRequest">The AsyncWorkRequest that is to be completed.</param> /// <param name="completionArguments">Completion arguments</param> public void CompleteWorkRequest(AsyncWorkRequest workRequest, object completionArguments) { if (workRequest.AsyncOperation == null) { throw new AsyncWorkManagerException("Cannot complete a worker WebRequest that hasnt been started yet."); } // Post the completion callback try { // почитай про асинк оператион но похоже что надо запусть в дргом потоке workRequest.AsyncOperation.PostOperationCompleted(new SendOrPostCallback(workRequest.CompletionCallback), completionArguments); } catch (InvalidOperationException) { } // Process the next request this.ProcessNextWorkRequest(); }
/// <summary> /// Called to process the next queued work item. /// </summary> public void ProcessNextWorkRequest() { /* * if (this.AllWorkersCompleted) * { * throw new AsyncWorkManagerException("Cannot restart an already completed queue manager."); * } */ if (this.CancelRequested) { // Check to see that the cancelled request has not been processed if (!this.Cancelled) { lock (this._lockObject) { if (!this.Cancelled) { CheckAndSendCancellationNotice(); } } } } else if (this._workersQueue.Count > 0) { AsyncWorkRequest workRequest = this._workersQueue.Dequeue(); AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(workRequest.Id); workRequest.SetAsyncOperation(asyncOp); ThreadPool.QueueUserWorkItem(new WaitCallback( o => { asyncOp.SynchronizationContext.Post(new SendOrPostCallback(LaunchWorkerCallback), workRequest); })); } else { // No workers were found. End the session if no session is established or if a session has been // established and has completed. if (!this._sessionEnabled || (this._sessionEnabled && this._sessionCompleted)) { this.EndSession(); } } }
/// <summary> /// Called when a new RefreshAsync is invoked by the user /// </summary> /// <param name="asyncWorker"></param> /// <param name="inputParams">An array of input params that can be passed to this method. Null in this case</param> void RefreshWorker(AsyncWorkRequest asyncWorker, object[] inputParams) { this._refreshRequestWorker = asyncWorker; refreshStats.StartTime = DateTime.Now; try { // First create the CacheRequestHandler #if MONOTOUCH this._cacheRequestHandler = new NSUrlCacheRequestHandler(_serviceUri, _controllerBehavior, _asyncWorkManager); #else this._cacheRequestHandler = new HttpCacheRequestHandler(_serviceUri, _controllerBehavior, _asyncWorkManager); #endif // Register for the ProcessRequestAsync callback of the CacheRequestHandler this._cacheRequestHandler.ProcessCacheRequestCompleted += new EventHandler <ProcessCacheRequestCompletedEventArgs>(ProcessCacheRequestCompleted); // Then fire the BeginSession call on the local provider. this._localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this._beginSessionComplete = true; // Dont enqueue another request if its been cancelled if (this.Cancelled) { this._asyncWorkManager.CheckAndSendCancellationNotice(); return; } // Do uploads first this.EnqueueUploadRequest(); } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } CompleteAsyncWithException(e); } }
/// <summary> /// Adds an AsyncWorkRequest to the queue. /// </summary> /// <param name="workRequest"></param> public void AddWorkRequest(AsyncWorkRequest workRequest) { this._workersQueue.Enqueue(workRequest); this.ProcessNextWorkRequest(); }
/// <summary> /// This method will be called on a background thread and will be called within the correct synchronization context. /// Now invoke the Asyc workers work callback on this thread. /// </summary> /// <param name="state"></param> void LaunchWorkerCallback(object state) { AsyncWorkRequest workRequest = state as AsyncWorkRequest; workRequest.WorkerCallback(workRequest, workRequest.InputParameters); }
void ProcessCacheRequestWorker(AsyncWorkRequest worker, object[] inputParams) { var request = inputParams[0] as CacheRequest; object state = inputParams[1]; _wrapper = new AsyncArgsWrapper { UserPassedState = state, WorkerRequest = worker, CacheRequest = request }; ProcessRequest(); }
/// <summary> /// Called when a new RefreshAsync is invoked by the user /// </summary> /// <param name="asyncWorker"></param> /// <param name="inputParams">An array of input params that can be passed to this method. Null in this case</param> void RefreshWorker(AsyncWorkRequest asyncWorker, object[] inputParams) { this._refreshRequestWorker = asyncWorker; refreshStats.StartTime = DateTime.Now; try { // First create the CacheRequestHandler this._cacheRequestHandler = CacheRequestHandler.CreateRequestHandler(this._serviceUri, this._controllerBehavior, this._asyncWorkManager); // Register for the ProcessRequestAsync callback of the CacheRequestHandler this._cacheRequestHandler.ProcessCacheRequestCompleted += new EventHandler<ProcessCacheRequestCompletedEventArgs>(ProcessCacheRequestCompleted); // Then fire the BeginSession call on the local provider. this._localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this._beginSessionComplete = true; // Dont enqueue another request if its been cancelled if (this.Cancelled) { this._asyncWorkManager.CheckAndSendCancellationNotice(); return; } // Do uploads first this.EnqueueUploadRequest(); } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } CompleteAsyncWithException(e); } }