如果您使用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
如果您停止下載,則需要重新啓動,您還需要跟蹤停止的位置,並可能下載並忽略這些部分,或重新開始下載。 –