Esempio n. 1
0
 public void Download(DownloadInfo info)
 {
     _downloadList.Enqueue(info);
     if (isStop)
     {
         isStop = false;
         thread = new Thread(DownloadThread);
         thread.IsBackground = true;
         thread.Start();
     }
 }
Esempio n. 2
0
        private void DownloadThread()
        {
            while (!isStop && _downloadList.Count > 0)
            {
                DownloadInfo info    = _downloadList.Dequeue();
                string       dirPath = Path.GetDirectoryName(info._path);
                if (!string.IsNullOrEmpty(dirPath) && !Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                FileStream fileStream = null;
                if (File.Exists(info._path))
                {
                    File.Delete(info._path);
                }
                fileStream = File.Create(info._path);
                long fileLength = fileStream.Length;
                curDownloadFile = info._path;
                float totalLength = GetLength(info._url);
                if (fileLength < totalLength)
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(info._url);
                    request.AddRange((int)fileLength);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    fileStream.Seek(fileLength, SeekOrigin.Begin);
                    Stream httpStream = response.GetResponseStream();
                    byte[] buffer     = new byte[1024];
                    int    length     = httpStream.Read(buffer, 0, buffer.Length);
                    while (length > 0)
                    {
                        if (isStop)
                        {
                            break;
                        }
                        fileStream.Write(buffer, 0, length);
                        fileLength += length;
                        progress    = fileLength / totalLength * 100;
                        fileStream.Flush();
                        length = httpStream.Read(buffer, 0, buffer.Length);
                    }
                    httpStream.Close();
                    httpStream.Dispose();
                }
                else
                {
                    progress = fileLength / totalLength * 100;
                }
                fileStream.Close();
                fileStream.Dispose();
                if (check != null)
                {
                    if (!check(info._sPath))
                    {
                        _downloadList.Enqueue(info);
                    }
                }
            }

            Close();
            if (callback != null)
            {
                callback();
            }
        }