2 using System.Collections.Generic;
\r
3 using System.ComponentModel;
\r
5 using System.Drawing;
\r
7 using System.Windows.Forms;
\r
10 using System.Threading;
\r
11 using System.Diagnostics;
\r
15 public partial class frmDownload : Form
\r
17 private Thread downloadThread;
\r
18 private Stream responceStream;
\r
19 private Stream loacalStream;
\r
20 private HttpWebRequest webRequest;
\r
21 private HttpWebResponse webResponse;
\r
22 private static int progress;
\r
23 private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
\r
24 private delegate void DownloadCompleteCallback();
\r
25 private delegate void DownloadFailedCallback();
\r
28 public frmDownload()
\r
30 InitializeComponent();
\r
34 downloadThread = new Thread(Download);
\r
35 downloadThread.Start();
\r
37 catch (Exception exc)
\r
39 MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
44 private void Download()
\r
46 Functions.RssReader rssRead = new Functions.RssReader();
\r
48 string appPath = Application.StartupPath.ToString() + "\\";
\r
49 string hbUpdate = rssRead.downloadFile();
\r
50 string downloadPath = appPath + "Handbrake-win.exe";
\r
51 WebClient wcDownload = new WebClient();
\r
54 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
55 webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
56 webResponse = (HttpWebResponse)webRequest.GetResponse();
\r
57 Int64 fileSize = webResponse.ContentLength;
\r
59 responceStream = wcDownload.OpenRead(hbUpdate);
\r
60 loacalStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
63 byte[] downBuffer = new byte[2048];
\r
66 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
68 loacalStream.Write(downBuffer, 0, bytesSize);
\r
69 flength = loacalStream.Length;
\r
70 this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });
\r
73 responceStream.Close();
\r
74 loacalStream.Close();
\r
76 if (flength != fileSize)
\r
77 this.Invoke(new DownloadFailedCallback(this.downloadFailed));
\r
79 this.Invoke(new DownloadCompleteCallback(this.downloadComplete));
\r
87 private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
\r
91 long p = (BytesRead * 100) / TotalBytes;
\r
92 progress = int.Parse(p.ToString());
\r
93 progress_download.Value = progress;
\r
94 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
\r
96 catch (Exception exc)
\r
98 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
102 private void downloadComplete()
\r
104 lblProgress.Text = "Download Complete";
\r
105 btn_cancel.Text = "Close";
\r
107 string appPath = Application.StartupPath.ToString() + "\\";
\r
109 Process hbproc = Process.Start(appPath + "Handbrake-win.exe");
\r
110 hbproc.WaitForExit();
\r
117 private void downloadFailed()
\r
119 lblProgress.Text = "Download Failed";
\r
120 btn_cancel.Text = "Close";
\r
123 private void btn_cancel_Click(object sender, EventArgs e)
\r
125 webResponse.Close();
\r
126 responceStream.Close();
\r
127 loacalStream.Close();
\r
128 downloadThread.Abort();
\r
129 progress_download.Value = 0;
\r
130 lblProgress.Text = "Download Stopped";
\r