Пример #1
0
        private void ResultCallback(IAsyncResult asynchResult)
        {
            if (!asynchResult.IsCompleted)
            {
                return;
            }

            AsyncRequestState asyncRequestState = null;
            HttpWebResponse   httpWebResponse   = null;

            try
            {
                asyncRequestState = (AsyncRequestState)asynchResult.AsyncState;

                this.Page.Dispatcher.BeginInvoke(() =>
                {
                    PI.IsVisible = false;
                });

                //Thread.Sleep(15000);  // uncomment this line to test the timeout condition

                HttpWebRequest httpWebRequest = (HttpWebRequest)asyncRequestState.request;
                httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asynchResult);

                if (httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    asyncRequestState.Success = false;
                    throw new WebException("EndGetResponse Status Code = " + httpWebResponse.StatusCode);
                }

                using (Stream responseStream = httpWebResponse.GetResponseStream())
                {
                    asyncRequestState.actionToPerform(responseStream);
                }


                this.Page.Dispatcher.BeginInvoke(() =>
                {
                    MessageBoxResult mbResult = MessageBox.Show("Your pass code has been set", "Information", MessageBoxButton.OK);
                    if (mbResult == MessageBoxResult.OK)
                    {
                        (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/SettingsView.xaml?PassCodeSet=True", UriKind.Relative));
                    }
                });
            }
            catch
            {
                //if (asyncRequestState != null && asyncRequestState.request != null)
                //    ShowErrorMessage();
            }

            if (httpWebResponse != null)
            {
                httpWebResponse.Close();
            }

            asyncRequestState.allDone.Set();
        }
Пример #2
0
        public void RegisterCodeForRecovery(string email, ProgressIndicator pi)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                AsyncRequest async = new AsyncRequest(pi, this.Page);
                AsyncRequestState asyncRequestState = new AsyncRequestState(async.XMLConsumption);
                async.Invoke(5000, asyncRequestState, string.Format("http://www.thinkbackpacking.com/DatingDiary/Register.php?email={0}&pin={1}", email, ((SetPassCode)Page).txtRetype.Text));

                //return new SetPassCodeResult() { Success = asyncRequestState.success, Message = asyncRequestState.success ? "Your pass code has been set" : "There was an error in connecting the remote server. Your pass code has not been set" };
            }
            else
                MessageBox.Show("You do not have any connectivity to complete the operation at this time", "Information", MessageBoxButton.OK);
                //return new SetPassCodeResult() { Success = false, Message = "You do not have any connectivity to complete the operation at this time" };
        }
Пример #3
0
        public void Invoke(int timeout_ms, AsyncRequestState asyncRequestState, string location)
        {
            try
            {
                this.PI.IsVisible = true;
                this.Page.Opacity = 0.3;

                ThreadPool.QueueUserWorkItem(new WaitCallback(target =>
                {

                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(location);
                    asyncRequestState.request = httpWebRequest;
                    asyncRequestState.allDone = new AutoResetEvent(false);

                    try
                    {
                        IAsyncResult result = (IAsyncResult)httpWebRequest.BeginGetResponse(new AsyncCallback(ResultCallback), asyncRequestState);

                        if (!asyncRequestState.allDone.WaitOne(timeout_ms))
                        {
                            if (httpWebRequest != null)
                            {
                                httpWebRequest.Abort();

                                ShowErrorMessage();
                            }
                        }

                    }
                    catch
                    {
                        ShowErrorMessage();
                    }
               }));
            }
            catch
            {
                ShowErrorMessage();
            }
        }
Пример #4
0
        public void Invoke(int timeout_ms, AsyncRequestState asyncRequestState, string location)
        {
            try
            {
                this.PI.IsVisible = true;
                this.Page.Opacity = 0.3;

                ThreadPool.QueueUserWorkItem(new WaitCallback(target =>
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(location);
                    asyncRequestState.request     = httpWebRequest;
                    asyncRequestState.allDone     = new AutoResetEvent(false);

                    try
                    {
                        IAsyncResult result = (IAsyncResult)httpWebRequest.BeginGetResponse(new AsyncCallback(ResultCallback), asyncRequestState);

                        if (!asyncRequestState.allDone.WaitOne(timeout_ms))
                        {
                            if (httpWebRequest != null)
                            {
                                httpWebRequest.Abort();

                                ShowErrorMessage();
                            }
                        }
                    }
                    catch
                    {
                        ShowErrorMessage();
                    }
                }));
            }
            catch
            {
                ShowErrorMessage();
            }
        }