gpt4 book ai didi

c# - 暂停下载线程

转载 作者:行者123 更新时间:2023-11-30 17:16:42 27 4
gpt4 key购买 nike

我正在用 C# 编写一个非常简单的批量下载程序,它读取 URL 的 .txt 文件进行下载。我已经设置了一个全局线程和委托(delegate)来更新 GUI,按下“开始”按钮会创建并启动该线程。我想要做的是有一个“暂停”按钮,使我能够暂停下载,直到我点击“恢复”按钮。我该怎么做?

相关代码:

private Thread thr;
private delegate void UpdateProgressCallback(int curFile);

private void Begin_btn_Click(object sender, EventArgs e)
{
thr = new Thread(Download);
thr.Start();
}

private void Pause_btn_Click(object sender, EventArgs e)
{
Pause_btn.Visible = false;
Resume_btn.Visible = true;
//{PAUSE THREAD thr}
}

private void Resume_btn_Click(object sender, Eventargs e)
{
Pause_btn.Visible = true;
Resume_btn.Visible = false;
//{RESUME THREAD thr}
}

public void Download()
{
//Download code goes here
}

显然,我没有使用 Worker,而且我真的不想使用,除非你能告诉我如何让它工作(我不太了解 Worker)。任何帮助将不胜感激。

最佳答案

如果您使用 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;
}
}
}
}

我使用了 HttpWebRequest.GetResponse 中的一些代码MSDN 上的页面。

除了在暂停时停止线程并在恢复时启动新线程,您还可以更改 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);
}

好处是您可以重复使用同一个线程。缺点是连接可能会超时并关闭。在后一种情况下,您将需要检测到这一点并重新连接。

您可能还想在下载完成时添加一个事件。

关于c# - 暂停下载线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com