示例#1
0
        async Task StartAsync()
        {
            try
            {
                var uri = SelectedMirrorProvider.GetUrlForMirror(SelectedFileMirror);

                using (var filter = await downloader.DownloadFilter(new Uri(uri), cancellationToken.Token, progress))
                {
                    cancellationToken.Token.ThrowIfCancellationRequested();

                    if (filter == null)
                    {
                        progress.Report(new ProgressModel(UpdateState.Cancelled, "A filter wasn't downloaded successfully.", 0));
                    }
                    else if (filter.Exception != null)
                    {
                        if (filter.Exception is OperationCanceledException)
                        {
                            throw filter.Exception;
                        }
                        Trace.TraceError("Problem when downloading: " + filter.Exception);
                        progress.Report(new ProgressModel(UpdateState.Cancelled, "Problem when downloading: " + filter.Exception.Message, 0));
                        return;
                    }
                    else
                    {
                        foreach (var application in apps)
                        {
                            Trace.TraceInformation("Updating app {0} {1}", application.Description, application.Version);
                            await application.Application.UpdateFilterAsync(filter, cancellationToken.Token, progress);
                        }
                    }

                    if (filter != null && filter.FilterTimestamp != null)
                    {
                        var message = $"Done. List timestamp: {filter.FilterTimestamp.Value.ToLocalTime()}";
                        Trace.TraceInformation(message);
                        progress.Report(new ProgressModel(UpdateState.Done, message, 100));
                    }
                    else
                    {
                        Trace.TraceInformation("Done.");
                        progress.Report(new ProgressModel(UpdateState.Done, "Done", 100));
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Trace.TraceWarning("Update was cancelled.");
                progress.Report(new ProgressModel(UpdateState.Cancelled, "Update was cancelled.", 0));
            }
            catch (Exception ex)
            {
                Trace.TraceError("Problem when updating: " + ex);
                progress.Report(new ProgressModel(UpdateState.Cancelled, "Problem when updating: " + ex.Message, 0));
            }
        }
示例#2
0
        internal async Task StartAsync()
        {
            var message = "Done";

            progress.Report(UpdateState.Downloading, "Starting...");

            try
            {
                if (SelectedMirrorProvider == null)
                {
                    progress.Report(new ProgressModel(UpdateState.Cancelled, "Please select a filter source", 0));
                    return;
                }

                var uri = SelectedMirrorProvider.GetUrlForMirror();

                using (var filter = await downloader.DownloadFilter(new Uri(uri), cancellationToken.Token, progress))
                {
                    cancellationToken.Token.ThrowIfCancellationRequested();

                    if (filter == null)
                    {
                        progress.Report(new ProgressModel(UpdateState.Cancelled, "A filter wasn't downloaded successfully.", 0));
                    }
                    else if (filter.Exception != null)
                    {
                        if (filter.Exception is OperationCanceledException)
                        {
                            throw filter.Exception;
                        }
                        Trace.TraceError("Problem when downloading: " + filter.Exception);
                        progress.Report(new ProgressModel(UpdateState.Cancelled, "Problem when downloading: " + filter.Exception.Message, 0));
                        return;
                    }
                    else
                    {
                        filter.Stream.Seek(0, SeekOrigin.Begin);
                        using (var reader = new StreamReader(filter.Stream, Encoding.Default, false, 65535, true))
                        {
                            var line = await reader.ReadLineAsync();

                            while (line != null)
                            {
                                var entry = DatParser.ParseEntry(line);
                                if (entry != null)
                                {
                                    filter.Entries.Add(entry);
                                }
                                var percent = (int)Math.Floor((double)filter.Stream.Position / filter.Stream.Length * 100);
                                await Task.Yield();

                                if (percent > ProgressValue)
                                {
                                    progress.Report(new ProgressModel(UpdateState.Decompressing, "Parsed " + filter.Entries.Count + " entries", percent));
                                }
                                line = await reader.ReadLineAsync();
                            }
                        }

                        foreach (var application in apps)
                        {
                            Trace.TraceInformation("Updating app {0} {1}", application.Description, application.Version);
                            await application.Application.UpdateFilterAsync(filter, cancellationToken.Token, progress);
                        }
                    }

                    if (filter?.FilterTimestamp != null)
                    {
                        message = $"Done. List timestamp: {filter.FilterTimestamp.Value.ToLocalTime()}";
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Trace.TraceWarning("Update was cancelled.");
                progress.Report(new ProgressModel(UpdateState.Cancelled, "Update was cancelled.", 0));
                return;
            }
            catch (Exception ex)
            {
                Trace.TraceError("Problem when updating: " + ex);
                progress.Report(new ProgressModel(UpdateState.Cancelled, "Problem when updating: " + ex.Message, 0));
                return;
            }

            progress.Report(UpdateState.Decompressing, "Cleaning up...", -1);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Trace.TraceInformation(message);
            progress.Report(new ProgressModel(UpdateState.Done, message, 100));
            ShowNotification("Updated IP Filter", message, ToolTipIcon.Info);
        }