예제 #1
0
 /// <summary>
 /// Cancels asynchronous requests.
 /// </summary>
 /// <remarks>
 /// Does not cancel requests created using XTaskAsync methods.
 /// </remarks>
 public virtual void CancelAsync()
 {
     lock (_httpWebRequestLocker)
     {
         if (_httpWebRequest != null)
         {
             _httpWebRequest.Abort();
         }
     }
 }
예제 #2
0
        protected virtual Task <object> ApiTaskAsync(HttpMethod httpMethod, string path, object parameters, Type resultType, object userState, CancellationToken cancellationToken
#if ASYNC_AWAIT
                                                     , IProgress <FacebookUploadProgressChangedEventArgs> uploadProgress
#endif
                                                     )
        {
            var tcs = new TaskCompletionSource <object>(userState);

            if (cancellationToken.IsCancellationRequested)
            {
                tcs.TrySetCanceled();
                return(tcs.Task);
            }

            HttpWebRequestWrapper httpWebRequest = null;
            EventHandler <HttpWebRequestCreatedEventArgs> httpWebRequestCreatedHandler = null;

            httpWebRequestCreatedHandler += (o, e) =>
            {
                if (e.UserState != tcs)
                {
                    return;
                }
                httpWebRequest = e.HttpWebRequest;
            };

            var ctr = cancellationToken.Register(() =>
            {
                try
                {
                    if (httpWebRequest != null)
                    {
                        httpWebRequest.Abort();
                    }
                }
                catch
                {
                }
            });

#if ASYNC_AWAIT
            EventHandler <FacebookUploadProgressChangedEventArgs> uploadProgressHandler = null;
            if (uploadProgress != null)
            {
                uploadProgressHandler = (sender, e) =>
                {
                    if (e.UserState != tcs)
                    {
                        return;
                    }
                    uploadProgress.Report(new FacebookUploadProgressChangedEventArgs(e.BytesReceived, e.TotalBytesToReceive, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage, userState));
                };

                UploadProgressChanged += uploadProgressHandler;
            }
#endif

            EventHandler <FacebookApiEventArgs> handler = null;
            handler = (sender, e) =>
            {
                TransferCompletionToTask(tcs, e, e.GetResultData, () =>
                {
                    if (ctr != null)
                    {
                        ctr.Dispose();
                    }
                    RemoveTaskAsyncHandlers(httpMethod, handler);
                    HttpWebRequestWrapperCreated -= httpWebRequestCreatedHandler;
#if ASYNC_AWAIT
                    if (uploadProgressHandler != null)
                    {
                        UploadProgressChanged -= uploadProgressHandler;
                    }
#endif
                });
            };

            if (httpMethod == HttpMethod.Get)
            {
                GetCompleted += handler;
            }
            else if (httpMethod == HttpMethod.Post)
            {
                PostCompleted += handler;
            }
            else if (httpMethod == HttpMethod.Delete)
            {
                DeleteCompleted += handler;
            }
            else
            {
                throw new ArgumentOutOfRangeException("httpMethod");
            }

            HttpWebRequestWrapperCreated += httpWebRequestCreatedHandler;

            try
            {
                ApiAsync(httpMethod, path, parameters, resultType, tcs);
            }
            catch
            {
                RemoveTaskAsyncHandlers(httpMethod, handler);
                HttpWebRequestWrapperCreated -= httpWebRequestCreatedHandler;
#if ASYNC_AWAIT
                if (uploadProgressHandler != null)
                {
                    UploadProgressChanged -= uploadProgressHandler;
                }
#endif
                throw;
            }

            return(tcs.Task);
        }