X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=win%2FC%23%2FfrmDownload.cs;h=a3067e17dac07577583e2c90c66fc3faa06186b1;hb=32219d93ec8fcf0db6f7ca5d6045419882563951;hp=73020ff1b00863fdfaf771244b03857ccc1166db;hpb=a12b7c4e65e4e32c79d2dbaa2705ecf5692c7b0f;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/win/C#/frmDownload.cs b/win/C#/frmDownload.cs index 73020ff1..a3067e17 100644 --- a/win/C#/frmDownload.cs +++ b/win/C#/frmDownload.cs @@ -1,127 +1,106 @@ /* frmDownload.cs $ - - This file is part of the HandBrake source code. - Homepage: . - It may be used under the terms of the GNU General Public License. */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Text; -using System.Windows.Forms; -using System.Net; -using System.IO; -using System.Threading; -using System.Diagnostics; + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ namespace Handbrake { + using System; + using System.Diagnostics; + using System.IO; + using System.Net; + using System.Threading; + using System.Windows.Forms; + public partial class frmDownload : Form { - private Thread downloadThread; - private Stream responceStream; - private Stream loacalStream; - private HttpWebRequest webRequest; - private HttpWebResponse webResponse; - private static int progress; - private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes); + private readonly Thread _downloadThread; + private Stream _responceStream; + private Stream _loacalStream; + private HttpWebRequest _webRequest; + private HttpWebResponse _webResponse; + private static int _progress; + private bool _killThread; + + private delegate void UpdateProgessCallback(long bytesRead, long totalBytes); + private delegate void DownloadCompleteCallback(); - private delegate void DownloadFailedCallback(); + private delegate void DownloadFailedCallback(); - public frmDownload() + public frmDownload(string filename) { InitializeComponent(); - try - { - downloadThread = new Thread(Download); - downloadThread.Start(); - } - catch (Exception exc) - { - MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error); - } - + _downloadThread = new Thread(Download); + _downloadThread.Start(filename); } - private void Download() + private void Download(object file) { - Functions.RssReader rssRead = new Functions.RssReader(); - string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"); + string hbUpdate = (string) file; + WebClient wcDownload = new WebClient(); - if (File.Exists(tempPath)) - File.Delete(tempPath); + try + { + if (File.Exists(tempPath)) + File.Delete(tempPath); - string hbUpdate = rssRead.downloadFile(); + _webRequest = (HttpWebRequest) WebRequest.Create(hbUpdate); + _webRequest.Credentials = CredentialCache.DefaultCredentials; + _webResponse = (HttpWebResponse) _webRequest.GetResponse(); + long fileSize = _webResponse.ContentLength; - WebClient wcDownload = new WebClient(); - try - { - webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate); - webRequest.Credentials = CredentialCache.DefaultCredentials; - webResponse = (HttpWebResponse)webRequest.GetResponse(); - Int64 fileSize = webResponse.ContentLength; - - responceStream = wcDownload.OpenRead(hbUpdate); - loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None); - - int bytesSize = 0; - byte[] downBuffer = new byte[2048]; - - long flength = 0; - while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0) - { - loacalStream.Write(downBuffer, 0, bytesSize); - flength = loacalStream.Length; - this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize }); - } - - responceStream.Close(); - loacalStream.Close(); - - if (flength != fileSize) - this.Invoke(new DownloadFailedCallback(this.downloadFailed)); - else - this.Invoke(new DownloadCompleteCallback(this.downloadComplete)); - } - catch (Exception) + _responceStream = wcDownload.OpenRead(hbUpdate); + _loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None); + + int bytesSize; + byte[] downBuffer = new byte[2048]; + + long flength = 0; + while ((bytesSize = _responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0) { - // Do Nothing + if (_killThread) + return; + _loacalStream.Write(downBuffer, 0, bytesSize); + flength = _loacalStream.Length; + Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] {_loacalStream.Length, fileSize}); } - } - private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes) - { - try - { - long p = (BytesRead * 100) / TotalBytes; - progress = int.Parse(p.ToString()); - progress_download.Value = progress; - lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k "; + _responceStream.Close(); + _loacalStream.Close(); + + if (flength != fileSize) + Invoke(new DownloadFailedCallback(this.DownloadFailed)); + else + Invoke(new DownloadCompleteCallback(this.DownloadComplete)); } - catch (Exception exc) + catch { - MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + // Do Nothing } } - private void downloadComplete() + private void UpdateProgress(long bytesRead, long totalBytes) + { + long p = (bytesRead * 100) / totalBytes; + int.TryParse(p.ToString(), out _progress); + progress_download.Value = _progress; + lblProgress.Text = (bytesRead / 1024) + "k of " + (totalBytes / 1024) + "k "; + } + + private void DownloadComplete() { lblProgress.Text = "Download Complete"; btn_cancel.Text = "Close"; - string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"); - - Process startInstall = Process.Start(tempPath); + Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe")); this.Close(); Application.Exit(); } - private void downloadFailed() + private void DownloadFailed() { lblProgress.Text = "Download Failed"; btn_cancel.Text = "Close"; @@ -129,20 +108,12 @@ namespace Handbrake private void btn_cancel_Click(object sender, EventArgs e) { - try - { - webResponse.Close(); - responceStream.Close(); - loacalStream.Close(); - downloadThread.Abort(); - progress_download.Value = 0; - lblProgress.Text = "Download Stopped"; - this.Close(); - } - catch (Exception) - { - // Do nothing - } + _killThread = true; + lblProgress.Text = "Cancelling ..."; + if (_webResponse != null) _webResponse.Close(); + if (_responceStream != null) _responceStream.Close(); + if (_loacalStream != null) _loacalStream.Close(); + this.Close(); } } } \ No newline at end of file