如果您使用System.Net.WebClient.DownloadFile()
或System.Net.WebClient.DownloadFileAsync()
方法那么您不能暂停下载。这些方法之间的区别在于后一种方法将启动异步下载,因此如果使用此方法,则不需要自己创建单独的线程。不幸的是,使用这两种方法执行的下载无法暂停或恢复。您需要使用System.Net.HttpWebRequest
。尝试这样的事情:
class Downloader
{
private const int chunkSize = 1024;
private bool doDownload = true;
private string url;
private string filename;
private Thread downloadThread;
public long FileSize
{
get;
private set;
}
public long Progress
{
get;
private set;
}
public Downloader(string Url, string Filename)
{
this.url = Url;
this.filename = Filename;
}
public void StartDownload()
{
Progress = 0;
FileSize = 0;
commenceDownload();
}
public void PauseDownload()
{
doDownload = false;
downloadThread.Join();
}
public void ResumeDownload()
{
doDownload = true;
commenceDownload();
}
private void commenceDownload()
{
downloadThread = new Thread(downloadWorker);
downloadThread.Start();
}
public void downloadWorker()
{
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
FileMode filemode;
// For download resume
if (Progress == 0)
{
filemode = FileMode.CreateNew;
}
else
{
filemode = FileMode.Append;
myHttpWebRequest.AddRange(Progress);
}
// Set up a filestream to write the file
// Sends the HttpWebRequest and waits for the response.
using (FileStream fs = new FileStream(filename, filemode))
using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
{
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
FileSize = myHttpWebResponse.ContentLength;
byte[] read = new byte[chunkSize];
int count;
while ((count = receiveStream.Read(read, 0, chunkSize)) > 0 && doDownload)
{
fs.Write(read, 0, count);
count = receiveStream.Read(read, 0, chunkSize);
Progress += count;
}
}
}
}
我在MSDN上使用了HttpWebRequest.GetResponse页面的一些代码。
而是停在暂停线程,并开始对恢复一个新的,你也可以改变while
循环要等到下载恢复如下:
while ((count = receiveStream.Read(read, 0, chunkSize)) > 0)
{
fs.Write(read, 0, count);
count = receiveStream.Read(read, 0, chunkSize);
Progress += count;
while(!doDownload)
System.Threading.Thread.Sleep(100);
}
的上侧是,你可能能够重新使用相同的线程。不利的一面是连接可能会超时并关闭。在后一种情况下,您需要检测并重新连接。
您可能还想为donwload完成时添加事件。
我不是C#专家,但我相信这将*非常*取决于您如何下载文件。 'Download()'中的实际代码是什么? – jtbandes
你可能不得不结束http连接并在简历上重新建立它。否则TCP将超时。 – sleeplessnerd
如果您停止下载,则需要重新启动,您还需要跟踪停止的位置,并可能下载并忽略这些部分,或重新开始下载。 –