为什么单击button1开始下载它永远不会继续?

时间:2017-03-08 15:25:01

标签: c# .net winforms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tester
{
    public partial class Form1 : Form
    {
        FileDownload fw;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Download()
        {
            fw = new FileDownload("http://download.microsoft.com/download/E/E/2/EE2D29A1-2D5C-463C-B7F1-40E4170F5E2C/KinectSDK-v1.0-Setup.exe", @"D:\KinetSDK.exe", 5120);

            // Display progress...
            Task.Factory.StartNew(() =>
            {
                while (!fw.Done)
                {
                    //Console.SetCursorPosition(0, Console.CursorTop);
                    label1.Text = string.Format("ContentLength: {0} | BytesWritten: {1}", fw.ContentLength, fw.BytesWritten);
                }
            });

            // Start the download...
            fw.Start();



            //Console.ReadKey();
        }

        public class FileDownload
        {
            private volatile bool _allowedToRun;
            private string _source;
            private string _destination;
            private int _chunkSize;

            private Lazy<int> _contentLength;

            public int BytesWritten { get; private set; }
            public int ContentLength { get { return _contentLength.Value; } }

            public bool Done { get { return ContentLength == BytesWritten; } }

            public FileDownload(string source, string destination, int chunkSize)
            {
                _allowedToRun = true;

                _source = source;
                _destination = destination;
                _chunkSize = chunkSize;
                _contentLength = new Lazy<int>(() => Convert.ToInt32(GetContentLength()));

                BytesWritten = 0;
            }

            private long GetContentLength()
            {
                var request = (HttpWebRequest)WebRequest.Create(_source);
                request.Method = "HEAD";

                using (var response = request.GetResponse())
                    return response.ContentLength;
            }

            private async Task Start(int range)
            {
                if (!_allowedToRun)
                    throw new InvalidOperationException();

                var request = (HttpWebRequest)WebRequest.Create(_source);
                request.Method = "GET";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                request.AddRange(range);

                using (var response = await request.GetResponseAsync())
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (var fs = new FileStream(_destination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                        {
                            while (_allowedToRun)
                            {
                                var buffer = new byte[_chunkSize];
                                var bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);

                                if (bytesRead == 0) break;

                                await fs.WriteAsync(buffer, 0, bytesRead);
                                BytesWritten += bytesRead;
                            }

                            await fs.FlushAsync();
                        }
                    }
                }
            }

            public Task Start()
            {
                _allowedToRun = true;
                return Start(BytesWritten);
            }

            public void Pause()
            {
                _allowedToRun = false;
            }
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (btnPause.Text == "Pause")
            {   
                fw.Pause();
                btnPause.Text = "Continue";
            }

            if (btnPause.Text == "Continue")
            {
                fw.Start().ContinueWith(t => label2.Text = "Done");
                btnPause.Text = "Pause";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Download();
        }
    }
}

使用断点进入该行:

using (var fs = new FileStream(_destination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))

当我点击继续时,我看到form1但没有任何反应。 label1没有进展,当我点击暂停按钮时没有任何反应。如果我在下一行的这一行之后设置一个断点,它就不会到达那里。

永远不会走到这一行:

while (_allowedToRun)

0 个答案:

没有答案