Ejemplo n.º 1
0
        public async static Task DownloadLatestUiInstaller(MainWindow mainWindow, string url, Action <bool, FileInfo> onDownloadUiInstallerComplete)
        {
            ProgressDialogController controller = null;

            try
            {
                var fileName = Path.GetFileName(new Uri(url).LocalPath);
                var filePath = new FileInfo(Path.Combine(FileSystem.DownloadDirectory.FullName, fileName));

                if (!filePath.Directory.Exists)
                {
                    filePath.Directory.Create();
                }
                if (filePath.Exists)
                {
                    filePath.Delete();
                }
                filePath.Refresh();

                controller = await mainWindow.ShowProgressAsync("Please wait...", "Downloading GUI installer", isCancelable : true);

                controller.SetProgressBarForegroundBrush(Brushes.Orange);
                controller.SetIndeterminate();
                controller.Maximum = 100d;

                await Task.Yield();

                await Task.Delay(200);

                var downloader = Network.DownloadFromURL(url, filePath.FullName,
                                                         new DownloadProgressChangedEventHandler((s, progressEvent) =>
                {
                    mainWindow.Invoke(() =>
                    {
                        var percentage = (double)progressEvent.BytesReceived / progressEvent.TotalBytesToReceive * 100;
                        controller.SetProgress(percentage);
                    });
                }),
                                                         new System.ComponentModel.AsyncCompletedEventHandler(async(s, completedEvent) =>
                {
                    await mainWindow.Invoke(async() =>
                    {
                        if (completedEvent.Error != null)
                        {
                            if (!(completedEvent.Error is WebException) || ((WebException)completedEvent.Error).Status != WebExceptionStatus.RequestCanceled)
                            {
                                ShowMessageBox("Error downloading GUI installer", completedEvent.Error.Message);
                                onDownloadUiInstallerComplete(false, null);
                            }
                        }
                        else
                        {
                            filePath.Refresh();
                            onDownloadUiInstallerComplete(true, filePath);
                        }
                        if (controller != null && controller.IsOpen)
                        {
                            await controller.CloseAsync();
                        }
                    });
                }));

                controller.Canceled += (s, cancelEvent) => downloader.CancelAsync();
                controller.Closed   += (s, closeEvent) => downloader.Dispose();
            }
            catch (Exception ex)
            {
                ShowMessageBox("Error downloading GUI installer", ex.Message);
            }
        }