www.pudn.com > Fetion.rar > Downloading.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Client.Utils; 
    using System; 
    using System.ComponentModel; 
    using System.Drawing; 
    using System.Threading; 
    using System.Windows.Forms; 
 
    public class Downloading : Form, IHandleUserStatusSelf 
    { 
        private long _downloaded; 
        private Downloader _downloader = new Downloader(); 
        private Thread _downloadThread; 
        private string _downloadUrl; 
        private UpdateInfo _updateInfo; 
        private WorkPathInfo _workPath = new WorkPathInfo(); 
        private Button buttonCancel; 
        private IContainer components; 
        private ProgressBar downloadProgress; 
        private LinkLabel lDownloadURL; 
        private Label lMessage; 
        private Panel panel1; 
 
        internal Downloading(UpdateInfo updateInfo, string downloadUrl) 
        { 
            this.InitializeComponent(); 
            this._updateInfo = updateInfo; 
            this._downloadUrl = downloadUrl; 
            this.downloadProgress.Maximum = 100; 
            this.downloadProgress.Value = 0; 
            this._downloader.PartDownloaded += new EventHandler(this.downloader_PartDownloaded); 
            this._downloader.EndDownload += new EventHandler(this.downloader_EndDownload); 
            this.lDownloadURL.Text = this._downloadUrl; 
        } 
 
        private void buttonCancel_Click(object sender, EventArgs e) 
        { 
            DialogResult result; 
            lock (this._downloader.DownloadLocker) 
            { 
                result = MessageBox.Show("更新正在进行中,您确定要退出吗?", "退出更新", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); 
            } 
            if (result == DialogResult.OK) 
            { 
                if (this._downloadThread != null) 
                { 
                    this._downloadThread.Abort(); 
                } 
                base.DialogResult = DialogResult.No; 
                base.Close(); 
            } 
        } 
 
        protected override void Dispose(bool disposing) 
        { 
            if (disposing && (this.components != null)) 
            { 
                this.components.Dispose(); 
            } 
            base.Dispose(disposing); 
        } 
 
        private void downloader() 
        { 
            try 
            { 
                string fullPath = StaticTools.GetFullPath(this._workPath.ClientUpdateFile); 
                this._downloader.Download(this._downloadUrl, fullPath); 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteGeneral("下载更新失败", exception.ToString(), 20); 
            } 
        } 
 
        private void downloader_EndDownload(object sender, EndDownloadedEventArgs e) 
        { 
            this.downloadProgress.Value = 100; 
            if (e.IsComplete) 
            { 
                base.DialogResult = DialogResult.Yes; 
                base.Close(); 
            } 
            else 
            { 
                using (DownloadError error = new DownloadError(this._downloadUrl)) 
                { 
                    if (error.ShowDialog() == DialogResult.Yes) 
                    { 
                        this._downloaded = 0L; 
                        this.downloadProgress.Value = 0; 
                        base.Activate(); 
                        this.StartDownloader(); 
                        return; 
                    } 
                } 
                base.DialogResult = DialogResult.No; 
                base.Close(); 
            } 
        } 
 
        private void downloader_PartDownloaded(object sender, PartDownloadedEventArgs e) 
        { 
            try 
            { 
                this._downloaded += e.Size; 
                this.downloadProgress.Value = (int) ((this._downloaded * 100L) / this._updateInfo.Size); 
            } 
            catch 
            { 
                this.downloadProgress.Value = 100; 
            } 
        } 
 
        private void Downloading_Load(object sender, EventArgs e) 
        { 
            this.StartDownloader(); 
        } 
 
        private void InitializeComponent() 
        { 
            ComponentResourceManager manager = new ComponentResourceManager(typeof(Downloading)); 
            this.downloadProgress = new ProgressBar(); 
            this.buttonCancel = new Button(); 
            this.lMessage = new Label(); 
            this.lDownloadURL = new LinkLabel(); 
            this.panel1 = new Panel(); 
            this.panel1.SuspendLayout(); 
            base.SuspendLayout(); 
            this.downloadProgress.Location = new System.Drawing.Point(12, 0x39); 
            this.downloadProgress.Name = "downloadProgress"; 
            this.downloadProgress.Size = new Size(410, 0x15); 
            this.downloadProgress.TabIndex = 0; 
            this.downloadProgress.Value = 50; 
            this.buttonCancel.Location = new System.Drawing.Point(0x1b6, 0x39); 
            this.buttonCancel.Name = "buttonCancel"; 
            this.buttonCancel.Size = new Size(0x4b, 0x15); 
            this.buttonCancel.TabIndex = 1; 
            this.buttonCancel.Text = "退出"; 
            this.buttonCancel.UseVisualStyleBackColor = true; 
            this.buttonCancel.Click += new EventHandler(this.buttonCancel_Click); 
            this.lMessage.AutoSize = true; 
            this.lMessage.Location = new System.Drawing.Point(12, 12); 
            this.lMessage.Name = "lMessage"; 
            this.lMessage.Size = new Size(0x10d, 12); 
            this.lMessage.TabIndex = 2; 
            this.lMessage.Text = "如果下载速度缓慢,您可以自行到以下网址下载:"; 
            this.lDownloadURL.AutoSize = true; 
            this.lDownloadURL.Location = new System.Drawing.Point(12, 0x21); 
            this.lDownloadURL.Name = "lDownloadURL"; 
            this.lDownloadURL.Size = new Size(0x47, 12); 
            this.lDownloadURL.TabIndex = 3; 
            this.lDownloadURL.TabStop = true; 
            this.lDownloadURL.Text = "DownloadURL"; 
            this.lDownloadURL.LinkClicked += new LinkLabelLinkClickedEventHandler(this.lDownloadURL_LinkClicked); 
            this.panel1.BackColor = Color.Transparent; 
            this.panel1.Controls.Add(this.lMessage); 
            this.panel1.Controls.Add(this.lDownloadURL); 
            this.panel1.Controls.Add(this.downloadProgress); 
            this.panel1.Controls.Add(this.buttonCancel); 
            this.panel1.Dock = DockStyle.Fill; 
            this.panel1.Location = new System.Drawing.Point(0, 0); 
            this.panel1.Name = "panel1"; 
            this.panel1.Size = new Size(0x20b, 0x58); 
            this.panel1.TabIndex = 4; 
            base.AutoScaleDimensions = new SizeF(6f, 12f); 
            base.AutoScaleMode = AutoScaleMode.Font; 
            base.ClientSize = new Size(0x20b, 0x58); 
            base.Controls.Add(this.panel1); 
            base.FormBorderStyle = FormBorderStyle.FixedToolWindow; 
            base.Icon = (Icon) manager.GetObject("$this.Icon"); 
            base.Name = "Downloading"; 
            base.StartPosition = FormStartPosition.CenterScreen; 
            this.Text = "正在下载更新"; 
            base.Load += new EventHandler(this.Downloading_Load); 
            this.panel1.ResumeLayout(false); 
            this.panel1.PerformLayout(); 
            base.ResumeLayout(false); 
        } 
 
        private void lDownloadURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
        { 
            ShellHelper.StartUrl(this._downloadUrl); 
        } 
 
        protected override void OnClosed(EventArgs e) 
        { 
            this._downloader.PartDownloaded -= new EventHandler(this.downloader_PartDownloaded); 
            this._downloader.EndDownload -= new EventHandler(this.downloader_EndDownload); 
            base.OnClosed(e); 
        } 
 
        private void StartDownloader() 
        { 
            this._downloadThread = new Thread(new ThreadStart(this.downloader)); 
            this._downloadThread.Start(); 
        } 
 
        bool IHandleUserStatusSelf.Handled 
        { 
            get 
            { 
                return true; 
            } 
        } 
    } 
}