Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Web.UI;
- using System.Threading;
- using System.Windows.Threading;
- using System.ComponentModel;
- using System.Net;
- namespace Download_Manager
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- DownloadFiles dlFiles = new DownloadFiles();
- BackgroundWorker bgWorker = new BackgroundWorker();
- DispatcherTimer downloadingTimer = new DispatcherTimer();
- int progressBarNumber = 0;
- //Url to be downloaded
- private void downloadLink(Uri link)
- {
- dlFiles.downloadQueue.Add(link);
- }
- private void txtDownloadLink_KeyDown(object sender, KeyEventArgs e)
- {
- //Maximum parallel downloads
- if (dlFiles.downloadQueue.Count >= 5)
- {
- MessageBox.Show("Queue is full");
- return;
- }
- var key = e.Key;
- //Enter Key
- if (key == Key.Return)
- {
- //Add download link to queue
- try
- {
- downloadLink(new Uri(txtDownloadLink.Text));
- //Enables progress bars based on how many files are needed to download
- switch (progressBarNumber)
- {
- case 0:
- pgBar1.Visibility = System.Windows.Visibility.Visible;
- break;
- case 1:
- pgBar2.Visibility = System.Windows.Visibility.Visible;
- break;
- case 2:
- pgBar3.Visibility = System.Windows.Visibility.Visible;
- break;
- case 3:
- pgBar4.Visibility = System.Windows.Visibility.Visible;
- break;
- case 4:
- pgBar5.Visibility = System.Windows.Visibility.Visible;
- break;
- }
- progressBarNumber++;
- }
- catch (UriFormatException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- downloadingTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
- downloadingTimer.Tick += downloadingTimer_Tick;
- }
- private void downloadingTimer_Tick(object sender, EventArgs e)
- {
- if (dlFiles.isDownloadComplete)
- {
- //Reset progress bars
- foreach (ProgressBar p in gridProgressBars.Children)
- {
- p.Visibility = System.Windows.Visibility.Hidden;
- p.Value = 0;
- //p.Maximum = 0;
- }
- downloadingTimer.Stop();
- dlFiles.isDownloadComplete = true;
- progressBarNumber = 0;
- return;
- }
- int percentComplete = dlFiles.percent;
- int totalBytesToRecieve = dlFiles.totalBytesToRecieve;
- //Which progress bar to work with
- //Download queue index = 0 Progressbar = 1 etc...
- switch(dlFiles.fileToDownloadIndex)
- {
- case 0:
- pgBar1.Value = percentComplete;
- pgBar1.Maximum = totalBytesToRecieve;
- break;
- case 1:
- pgBar2.Value = percentComplete;
- pgBar2.Maximum = totalBytesToRecieve;
- break;
- case 2:
- pgBar3.Value = percentComplete;
- pgBar3.Maximum = totalBytesToRecieve;
- break;
- case 3:
- pgBar4.Value = percentComplete;
- pgBar4.Maximum = totalBytesToRecieve;
- break;
- case 4:
- pgBar5.Value = percentComplete;
- pgBar5.Maximum = totalBytesToRecieve;
- break;
- }
- }
- //Begin Download
- private void btnStartDownload_Click(object sender, RoutedEventArgs e)
- {
- if(downloadingTimer.IsEnabled)
- {
- MessageBox.Show("Download has already started");
- return;
- }
- if(dlFiles.downloadQueue.Count == 0)
- {
- MessageBox.Show("Queue is empty");
- return;
- }
- dlFiles.startDownload();
- downloadingTimer.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement