private void FetchDownloadButton_Click(object sender, RoutedEventArgs e) { if (fetchDownloadEnum == FetchDownloadCancel.Fetch) { //Check if thread is valid and populate info //Replace true with populate info subFolderName = GetSubFolderName(URLTextBox.Text); imagesDictionary = Downloader.ViewThread(URLTextBox.Text); PopulateDataGridView(); if (imagesDictionary.Count > 0) { isFetchValid = true; FetchDownloadButton.Content = "Download"; fetchDownloadEnum = FetchDownloadCancel.Download; //Prevents users from accessing it until done CancelButton.Visibility = Visibility.Visible; URLTextBox.IsEnabled = false; } else { MessageBox.Show("Error: Cannot start until a valid thread is inputted."); } } else if (fetchDownloadEnum == FetchDownloadCancel.Download) { if (isFetchValid == true) { backgroundWorker.RunWorkerAsync(); FetchDownloadButton.Content = "Cancel"; fetchDownloadEnum = FetchDownloadCancel.Cancel; } else { MessageBox.Show("Error: Cannot start until a valid thread is inputted."); } } else if (fetchDownloadEnum == FetchDownloadCancel.Cancel) { if (backgroundWorker.IsBusy) { backgroundWorker.CancelAsync(); } FetchDownloadButton.Content = "Fetch"; isFetchValid = false; fetchDownloadEnum = FetchDownloadCancel.Fetch; //Prevents users from accessing it until done CancelButton.Visibility = Visibility.Collapsed; URLTextBox.IsEnabled = true; } UpdateLabelFileSize(); }
private void CancelButton_Click(object sender, RoutedEventArgs e) { if (backgroundWorker.IsBusy) { backgroundWorker.CancelAsync(); } FetchDownloadButton.Content = "Fetch"; fetchDownloadEnum = FetchDownloadCancel.Fetch; //Prevents users from accessing it until done CancelButton.Visibility = Visibility.Collapsed; URLTextBox.IsEnabled = true; UpdateLabelFileSize(); }
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { //Only runs if downloadDirectory exists if (!string.IsNullOrEmpty(Properties.Settings.Default.downloadDirectory)) { BackgroundWorker worker = sender as BackgroundWorker; string directoryToSaveTo; if (Properties.Settings.Default.createSubFolderOnDownload) { //Check if subdirectory exists if not it creates it directoryToSaveTo = Properties.Settings.Default.downloadDirectory + "\\" + subFolderName; DirectoryInfo destinationDirectory = new DirectoryInfo(directoryToSaveTo); if (!destinationDirectory.Exists) { destinationDirectory.Create(); } } else { directoryToSaveTo = Properties.Settings.Default.downloadDirectory; } for (int i = 0; i < imagesDictionary.Count; i++) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { string result; if (dt.Rows[i].Field <bool>(0) == true) { result = Downloader.DownloadImage(imagesDictionary[i].getURLOfImage(), imagesDictionary[i].getNameOfImage(), imagesDictionary[i].getFileTypeOfImage(), directoryToSaveTo); } else { result = "SKIPPED"; } //Shows success or fail for each image DataGrid.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { if (!dt.Rows[i - 1].IsNull(5)) { dt.Rows[i - 1].SetField(5, result); } })); } int returnProgressPercent = (int)Math.Ceiling((decimal)(i + 1) / imagesDictionary.Count * 100); worker.ReportProgress(returnProgressPercent); } //Resets button on completion isFetchValid = false; fetchDownloadEnum = FetchDownloadCancel.Fetch; FetchDownloadButton.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { FetchDownloadButton.Content = "Fetch"; })); CancelButton.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { CancelButton.Visibility = Visibility.Collapsed; })); URLTextBox.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { URLTextBox.IsEnabled = true; })); //When done either exits or shows message or resumes if (Properties.Settings.Default.exitOnComplete) { Environment.Exit(0); } else if (Properties.Settings.Default.showMessageBoxOnComplete) { MessageBox.Show("Success! All files downloaded sucessfully"); } } else { MessageBox.Show("Error: Cannot start program until download directory is specified in Settings"); } }