コード例 #1
0
ファイル: Form1.cs プロジェクト: bangush/csharp
        private void DownloadParts(Uri uri)
        {
            myProgressBar1.Visible    = true;
            myProgressBar1.Progresses = new Dictionary <string, int>();

            HttpWebRequest wr = WebRequest.Create(uri) as HttpWebRequest;

            wr.Method = "HEAD";

            wr.AddRange(0);
            WebResponse wrs = wr.GetResponse();

            string filename = wrs.ResponseUri.Segments.Last();
            long   l        = wrs.ContentLength;

            int part = int.Parse(Math.Floor(l / parts).ToString());

            partsready = new Dictionary <int, DownPartClass>();
            for (int i = 0; i < parts; i++)
            {
                myProgressBar1.Progresses.Add("part_" + i, 0);
                DownPartClass dpc = new DownPartClass {
                    uri = uri, i = i, filename = filename, part = part
                };
                partsready.Add(i, dpc);
                ThreadPool.QueueUserWorkItem(DownPart, dpc);
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: Hagser/csharp
        private void DownloadParts(Uri uri)
        {

            myProgressBar1.Visible = true;
            myProgressBar1.Progresses = new Dictionary<string, int>();

            HttpWebRequest wr = WebRequest.Create(uri) as HttpWebRequest;
            wr.Method = "HEAD";

            wr.AddRange(0);
            WebResponse wrs = wr.GetResponse();

            string filename = wrs.ResponseUri.Segments.Last();
            long l = wrs.ContentLength;

            int part = int.Parse(Math.Floor(l / parts).ToString());
            partsready = new Dictionary<int, DownPartClass>();
            for (int i = 0; i < parts; i++)
            {
                myProgressBar1.Progresses.Add("part_"+i, 0);
                DownPartClass dpc = new DownPartClass { uri = uri, i = i, filename = filename, part = part };
                partsready.Add(i, dpc);
                ThreadPool.QueueUserWorkItem(DownPart, dpc);
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: bangush/csharp
        private void DownPart(object state)
        {
            DownPartClass dpc = state as DownPartClass;

            try
            {
                this.Invoke((MethodInvoker) delegate
                {
                    myProgressBar1.ChangeColor("part_" + dpc.i, Color.Green);
                });
                string file   = Application.CommonAppDataPath + "\\" + dpc.filename + "_" + dpc.i;
                long   istart = 0;
                if (File.Exists(file))
                {
                    //File.Delete(file);
                    istart = (new FileInfo(file)).Length;
                }

                if (istart < dpc.part)
                {
                    HttpWebRequest wrl = WebRequest.Create(dpc.uri) as HttpWebRequest;
                    wrl.Method = "GET";
                    wrl.AddRange((dpc.i * dpc.part) + istart, (dpc.i + 1) * dpc.part);
                    wrl.ReadWriteTimeout = int.Parse(Math.Floor((new TimeSpan(1, 0, 0)).TotalMilliseconds).ToString());
                    wrl.Timeout          = wrl.ReadWriteTimeout;
                    WebResponse wrsl = wrl.GetResponse();

                    Stream s = wrsl.GetResponseStream();

                    using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
                    {
                        byte[] buffer = new byte[650000];
                        int    icnt   = 0;
                        while ((icnt = s.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fs.Write(buffer, 0, icnt);
                            partsready[dpc.i].progress += icnt;
                            if (bCancel)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    partsready[dpc.i].progress = int.Parse(istart.ToString());
                }
                partsready[dpc.i].done = true;
            }
            catch (Exception ex) {
                dpc.retries++;
                partsready[dpc.i].retries = dpc.retries;
                partsready[dpc.i].error   = ex;
                partsready[dpc.i].paused  = true;

                this.Invoke((MethodInvoker) delegate
                {
                    myProgressBar1.ChangeColor("part_" + dpc.i, Color.Yellow);
                });
                if (dpc.retries >= 5)
                {
                    dpc.done = true;
                    this.Invoke((MethodInvoker) delegate
                    {
                        myProgressBar1.ChangeColor("part_" + dpc.i, Color.Red);
                    });
                }
            }
            if (dpc.part > dpc.progress && dpc.error == null && dpc.retries < 5)
            {
                dpc.paused = true;
                dpc.done   = false;
            }
            var pr = partsready.FirstOrDefault(x => x.Value.paused && !x.Value.done);

            if (pr.Value != null)
            {
                ThreadPool.QueueUserWorkItem(DownPart, pr.Value);
            }

            if (!bCancel && partsready.All(x => x.Value.done))
            {
                AssembleFiles(dpc.filename);
            }
        }