/// <summary> /// Searches for updates. /// </summary> /// <returns>Returns <c>true</c> if updates were found; otherwise, <c>false</c>.</returns> /// <exception cref="SizeCalculationException">The calculation of the size of the available updates has failed.</exception> /// <exception cref="OperationCanceledException">The update search was canceled.</exception> public bool SearchForUpdates() { if (_searchCancellationTokenSource != null) _searchCancellationTokenSource.Dispose(); _searchCancellationTokenSource = new CancellationTokenSource(); if (!ConnectionChecker.IsConnectionAvailable()) return false; // Check for SSL and ignore it ServicePointManager.ServerCertificateValidationCallback += delegate { return (true); }; var configuration = UpdateConfiguration.Download(_updateConfigurationFileUri, Proxy); var result = new UpdateResult(configuration, CurrentVersion, IncludeAlpha, IncludeBeta); if (!result.UpdatesFound) return false; _updateConfigurations = result.NewestConfigurations; double updatePackageSize = 0; foreach (var updateConfiguration in _updateConfigurations) { var newPackageSize = GetUpdatePackageSize(updateConfiguration.UpdatePackageUri); if (newPackageSize == null) throw new SizeCalculationException(_lp.PackageSizeCalculationExceptionText); updatePackageSize += newPackageSize.Value; _packageOperations.Add(new UpdateVersion(updateConfiguration.LiteralVersion), updateConfiguration.Operations); } TotalSize = updatePackageSize; if (_searchCancellationTokenSource.Token.IsCancellationRequested) { _packageOperations.Clear(); _packageFilePaths.Clear(); throw new OperationCanceledException(); } return true; }
/// <summary> /// Searches for updates, asynchronously. /// </summary> /// <seealso cref="SearchForUpdates" /> public void SearchForUpdatesAsync() { OnUpdateSearchStarted(this, EventArgs.Empty); Task.Factory.StartNew(() => { // It may be that this is not the first search call and previously saved data needs to be disposed. Cleanup(); // Reinitialize the cancellation tokens and wait handles _searchCancellationTokenSource?.Dispose(); _searchCancellationTokenSource = new CancellationTokenSource(); _searchManualResetEvent.Reset(); if (!ConnectionManager.IsConnectionAvailable()) { return(false); } ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; IEnumerable <UpdateConfiguration> configurations = null; Exception exception = null; UpdateConfiguration.DownloadAsync(UpdateConfigurationFileUri, HttpAuthenticationCredentials, Proxy, (c, e) => { configurations = c; exception = e; _searchManualResetEvent.Set(); }, _searchCancellationTokenSource, SearchTimeout); _searchManualResetEvent.WaitOne(); // Check for cancellation before throwing any errors if (_searchCancellationTokenSource.IsCancellationRequested) { return(false); } if (exception != null) { throw exception; } var result = new UpdateResult(configurations, CurrentVersion, IncludeAlpha, IncludeBeta, Conditions); if (!result.UpdatesFound) { return(false); } PackageConfigurations = result.NewestConfigurations; double updatePackageSize = 0; foreach (var updateConfiguration in PackageConfigurations) { updateConfiguration.UpdatePackageUri = ConvertPackageUri(updateConfiguration.UpdatePackageUri); updateConfiguration.UpdatePhpFileUri = ConvertStatisticsUri(updateConfiguration.UpdatePhpFileUri); if (_searchCancellationTokenSource.IsCancellationRequested) { return(false); } var newPackageSize = GetUpdatePackageSize(updateConfiguration.UpdatePackageUri); if (newPackageSize == null) { throw new SizeCalculationException(_lp.PackageSizeCalculationExceptionText); } updatePackageSize += newPackageSize.Value; if (updateConfiguration.Operations == null) { continue; } if (_packageOperations == null) { _packageOperations = new Dictionary <UpdateVersion, IEnumerable <Operation> >(); } _packageOperations.Add(new UpdateVersion(updateConfiguration.LiteralVersion), updateConfiguration.Operations); } TotalSize = updatePackageSize; return(true); }).ContinueWith(SearchTaskCompleted); }
/// <summary> /// Searches for updates, asynchronously. /// </summary> /// <seealso cref="SearchForUpdates" /> /// <exception cref="SizeCalculationException" /> /// <exception cref="OperationCanceledException" /> public Task <bool> SearchForUpdatesAsync() { return(TaskEx.Run(async() => { // It may be that this is not the first search call and previously saved data needs to be disposed. Cleanup(); _searchCancellationTokenSource?.Dispose(); _searchCancellationTokenSource = new CancellationTokenSource(); if (!ConnectionManager.IsConnectionAvailable()) { return false; } _searchCancellationTokenSource.Token.ThrowIfCancellationRequested(); ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; var configuration = await UpdateConfiguration.DownloadAsync(UpdateConfigurationFileUri, HttpAuthenticationCredentials, Proxy, _searchCancellationTokenSource, SearchTimeout); _searchCancellationTokenSource.Token.ThrowIfCancellationRequested(); var result = new UpdateResult(configuration, CurrentVersion, IncludeAlpha, IncludeBeta, Conditions); if (!result.UpdatesFound) { return false; } PackageConfigurations = result.NewestConfigurations; double updatePackageSize = 0; foreach (var updateConfiguration in PackageConfigurations) { updateConfiguration.UpdatePackageUri = ConvertPackageUri(updateConfiguration.UpdatePackageUri); updateConfiguration.UpdatePhpFileUri = ConvertStatisticsUri(updateConfiguration.UpdatePhpFileUri); _searchCancellationTokenSource.Token.ThrowIfCancellationRequested(); var newPackageSize = GetUpdatePackageSize(updateConfiguration.UpdatePackageUri); if (newPackageSize == null) { throw new SizeCalculationException(_lp.PackageSizeCalculationExceptionText); } updatePackageSize += newPackageSize.Value; if (updateConfiguration.Operations == null) { continue; } if (_packageOperations == null) { _packageOperations = new Dictionary <UpdateVersion, IEnumerable <Operation> >(); } _packageOperations.Add(new UpdateVersion(updateConfiguration.LiteralVersion), updateConfiguration.Operations); } TotalSize = updatePackageSize; if (!_searchCancellationTokenSource.Token.IsCancellationRequested) { return true; } throw new OperationCanceledException(); })); }