private void UpdateCheckerCallback(UpdateChecker uc, UpdateCheckerResult result) { try { if (!result.Success) { return; } if (CheckCompleted != null) { CheckCompleted(this, new UpdateCheckCompletedEventArgs() { HasUpdates = result.Updates.Count > 0 }); } if (result.Updates.Count > 0) { var answer = MessageBox.Show("Обнаружены новые версии. Обновить программу?", "Обновление", MessageBoxButton.YesNo, MessageBoxImage.Question); if (answer == MessageBoxResult.Yes) { var UpdWnd = new UpdatesWnd(); UpdWnd.Updates = result.Updates; UpdWnd.Show(); } } } catch { #if DEBUG throw; #endif } }
void DownloadFileCompleted(IAsyncResult asynchronousResult) { RequestState state = (RequestState)asynchronousResult.AsyncState; UpdateCheckerResult CheckResult = new UpdateCheckerResult(); try { HttpWebRequest myHttpWebRequest = (HttpWebRequest)state.request; state.response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult); if (state.response.ContentType == "application/xml") { CheckResult.Updates = LoadResult(state.response); CheckResult.Success = true; } else { CheckUpdateOnQueueAsync(state.ResultHandler, state); return; } } catch (Exception exc) { try { state.LastError = exc.ToString(); CheckUpdateOnQueueAsync(state.ResultHandler, state); return; } catch (Exception innerExc) { CheckResult.Exception = innerExc; CheckResult.Success = false; } } finally { if (state.response != null) { state.response.Close(); } } state.ResultHandler(this, CheckResult); }
private void CheckUpdateOnQueueAsync(UpdateCheckerResultHandler ResultHandler, RequestState state) { var reqState = (RequestState)state; string url; try { url = reqState.Urls.Dequeue(); } catch (InvalidOperationException) { var result = new UpdateCheckerResult(); result.Success = false; if (reqState.LastError != null) { throw new UpdaterException(reqState.LastError); } else { throw new UpdaterException("Ни один из адресов автообновления не вернул корректный ответ"); } } HttpWebRequest client = (HttpWebRequest)HttpWebRequest.Create(url); client.Accept = "application/xml"; client.UserAgent = "DBManager Autoupdate " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); client.AllowAutoRedirect = true; client.Proxy = WebRequest.DefaultWebProxy; client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; state.request = client; state.ResultHandler = ResultHandler; client.BeginGetResponse(GUIContextCallback(DownloadFileCompleted), reqState); }