暂停下载线程
我正在用 C# 编写一个非常简单的批量下载程序,该程序读取要下载的 URL 的 .txt 文件。我已经设置了一个全局线程和委托来更新 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)。任何帮助将不胜感激。
I'm writing a very simple bulk download program in c# that reads a .txt file of URLs to download. I've set it up with a global Thread and delegate for updating the GUI, and the pressing of the "Begin" button creates and starts that thread. What I want to do is have a "Pause" button that enables me to pause the download until I hit the "Resume" button. How do I do this?
The relevant code:
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
}
Obviously, I am NOT using a Worker, and I really don't wish to unless you can show me how to get it to work (I don't really understand workers). Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 System.Net.WebClient.DownloadFile() 或 System.Net.WebClient.DownloadFileAsync() 方法,则您无法暂停下载。这些方法之间的区别在于,后一种方法将启动异步下载,因此如果使用此方法,则不需要自己创建单独的线程。不幸的是,使用这两种方法执行的下载都无法暂停或恢复。
您需要使用
System.Net.HttpWebRequest
。尝试这样的事情:我使用了 HttpWebRequest.GetResponse 中的一些代码MSDN 上的 页面。
您还可以更改
while
循环以等待下载恢复,而不是在暂停时停止线程并在恢复时启动新线程,如下所示:好处是您可以重新开始- 使用相同的线程。缺点是连接可能会超时并关闭。在后一种情况下,您需要检测到这一点并重新连接。
您可能还想添加下载完成时的事件。
If you use
System.Net.WebClient.DownloadFile()
orSystem.Net.WebClient.DownloadFileAsync()
method then you cannot pause the download. The difference between these methods is that the latter method will start an asynchronous download so you will not need to create a separate thread yourself if you use this method. Unfortunately, downloads executed with either method cannot be paused or resumed.You need to use
System.Net.HttpWebRequest
. Try something like this:I used some code from HttpWebRequest.GetResponse page on MSDN.
Instead of stopping the thread on Pause and starting a new one on Resume, you can also change the
while
loop to wait until download is resumed as following:The up-side is that you may be able to re-use the same thread. The down-side is that the connection may timeout and become closed. In the latter case, you will need to detect this and re-connect.
You may also want to add an event for when the donwload is completed.