Advertisement
Guest User

Main Form

a guest
Apr 9th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Web.UI;
  16. using System.Threading;
  17. using System.Windows.Threading;
  18. using System.ComponentModel;
  19. using System.Net;
  20.  
  21. namespace Download_Manager
  22. {
  23.     /// <summary>
  24.     /// Interaction logic for MainWindow.xaml
  25.     /// </summary>
  26.     public partial class MainWindow : Window
  27.     {
  28.         public MainWindow()
  29.         {
  30.             InitializeComponent();
  31.            
  32.          
  33.            
  34.         }
  35.        
  36.         DownloadFiles dlFiles = new DownloadFiles();
  37.         BackgroundWorker bgWorker = new BackgroundWorker();
  38.         DispatcherTimer downloadingTimer = new DispatcherTimer();
  39.  
  40.        int progressBarNumber = 0;
  41.        
  42.         //Url to be downloaded
  43.         private void downloadLink(Uri link)
  44.         {
  45.             dlFiles.downloadQueue.Add(link);
  46.            
  47.         }
  48.        
  49.         private void txtDownloadLink_KeyDown(object sender, KeyEventArgs e)
  50.         {
  51.  
  52.             //Maximum parallel downloads
  53.             if (dlFiles.downloadQueue.Count >= 5)
  54.             {
  55.                 MessageBox.Show("Queue is full");
  56.                 return;
  57.             }
  58.  
  59.          
  60.             var key = e.Key;
  61.  
  62.             //Enter Key
  63.             if (key == Key.Return)
  64.             {
  65.                 //Add download link to queue
  66.                 try
  67.                 {
  68.                     downloadLink(new Uri(txtDownloadLink.Text));
  69.                    
  70.                     //Enables progress bars based on how many files are needed to download
  71.                     switch (progressBarNumber)
  72.                     {
  73.                         case 0:
  74.                             pgBar1.Visibility = System.Windows.Visibility.Visible;
  75.                             break;
  76.                         case 1:
  77.                             pgBar2.Visibility = System.Windows.Visibility.Visible;
  78.                             break;
  79.                         case 2:
  80.                             pgBar3.Visibility = System.Windows.Visibility.Visible;
  81.                             break;
  82.                         case 3:
  83.                             pgBar4.Visibility = System.Windows.Visibility.Visible;
  84.                             break;
  85.                         case 4:
  86.                             pgBar5.Visibility = System.Windows.Visibility.Visible;
  87.                             break;
  88.                     }
  89.  
  90.                     progressBarNumber++;
  91.                    
  92.                    
  93.                 }
  94.                 catch (UriFormatException ex)
  95.                 {
  96.                     MessageBox.Show(ex.Message);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         private void Window_Loaded(object sender, RoutedEventArgs e)
  102.         {
  103.             downloadingTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  104.             downloadingTimer.Tick += downloadingTimer_Tick;
  105.         }
  106.      
  107.  
  108.            
  109.        
  110.         private void downloadingTimer_Tick(object sender, EventArgs e)
  111.         {
  112.            
  113.             if (dlFiles.isDownloadComplete)
  114.             {
  115.  
  116.                 //Reset progress bars
  117.                 foreach (ProgressBar p in gridProgressBars.Children)
  118.                 {
  119.                     p.Visibility = System.Windows.Visibility.Hidden;
  120.                     p.Value = 0;
  121.                     //p.Maximum = 0;
  122.                 }
  123.                 downloadingTimer.Stop();
  124.                 dlFiles.isDownloadComplete = true;
  125.                 progressBarNumber = 0;
  126.                 return;
  127.             }
  128.  
  129.             int percentComplete = dlFiles.percent;
  130.             int totalBytesToRecieve = dlFiles.totalBytesToRecieve;
  131.  
  132.             //Which progress bar to work with
  133.             //Download queue index = 0 Progressbar = 1 etc...
  134.             switch(dlFiles.fileToDownloadIndex)
  135.             {
  136.                 case 0:
  137.                     pgBar1.Value = percentComplete;
  138.                     pgBar1.Maximum = totalBytesToRecieve;
  139.                     break;
  140.                 case 1:
  141.                     pgBar2.Value = percentComplete;
  142.                     pgBar2.Maximum = totalBytesToRecieve;
  143.                     break;
  144.                 case 2:
  145.                     pgBar3.Value = percentComplete;
  146.                     pgBar3.Maximum = totalBytesToRecieve;
  147.                     break;
  148.                 case 3:
  149.                     pgBar4.Value = percentComplete;
  150.                     pgBar4.Maximum = totalBytesToRecieve;
  151.                     break;
  152.                 case 4:
  153.                     pgBar5.Value = percentComplete;
  154.                     pgBar5.Maximum = totalBytesToRecieve;
  155.                     break;
  156.             }
  157.         }
  158.  
  159.         //Begin Download
  160.         private void btnStartDownload_Click(object sender, RoutedEventArgs e)
  161.         {
  162.             if(downloadingTimer.IsEnabled)
  163.             {
  164.                 MessageBox.Show("Download has already started");
  165.                 return;
  166.             }
  167.             if(dlFiles.downloadQueue.Count == 0)
  168.             {
  169.                 MessageBox.Show("Queue is empty");
  170.                 return;
  171.             }
  172.             dlFiles.startDownload();
  173.             downloadingTimer.Start();
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement