我正在尝试制作一个简单的下载管理器。它有效。问题是如果我添加一个进度条来通知用户我得到了这个异常
此流不支持搜索操作
while ((readed = ReadFrom.Read(read, 0, read.Length)) > 0)
{
strLocal.Write(read, 0, readed);
**UpdateProgress(ReadFrom.Length, strLocal.Length);// throws exception**
}
这是所有代码
private void button1_Click(object sender, EventArgs e)
{
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
res = (HttpWebResponse)req.GetResponse();
Int64 contentlength = res.ContentLength;
WebClient wc = new WebClient();
Stream ReadFrom= wc.OpenRead(new Uri(url));
strLocal = new FileStream(@"E:\\xxx.tar.gz", FileMode.Create, FileAccess.Write, FileShare.None);
byte[]read=new byte[1024];
int readed = 0;
while ((readed = ReadFrom.Read(read, 0, read.Length)) > 0)
{
strLocal.Write(read, 0, readed);
UpdateProgress(ReadFrom.Length, strLocal.Length);
}
strLocal.Close();
ReadFrom.Close();
}
private void UpdateProgress(long Filesize, long readedBytes)
{
// progressBar1.Maximum = 100;
int downloaded =Convert.ToInt32((readedBytes * 100) / Filesize);
progressBar1.Value = downloaded;
}
这里有什么问题?
@Edit
HttpWebRequest req;
HttpWebResponse res;
Stream strLocal;
Stream ResponseStr;
byte[] bytes = null;
Thread thr;
delegate void UpdateCallBack(long l1, long l2);
public Form1()
{
InitializeComponent();
}
string url="https://www.unrealircd.org/downloads/Unreal3.2.10.3.tar.gz";
////string url2 = "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34794&6B49FDFB-8E5B-4B07-BC31-15695C5A2143=1";
string filename = @"C:\\Unreal3.2.10.3.tar.gz";
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
thr = new Thread(download);
thr.Start();
}
private void download()
{
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
res = (HttpWebResponse)req.GetResponse();
Int64 contentlength = res.ContentLength;
ResponseStr = res.GetResponseStream();
strLocal = new FileStream(@"E:\\xxx.tar.gz", FileMode.Create, FileAccess.Write, FileShare.None);
byte[]read=new byte[1024];
int readed = 0;
while ((readed = ResponseStr.Read(read, 0, read.Length)) > 0)
{
strLocal.Write(read, 0, readed);
this.Invoke(new UpdateCallBack(this.UpdateProgress), new object[] {ResponseStr.Length,strLocal.Length });
}
strLocal.Close();
ResponseStr.Close();
}
private void UpdateProgress(long Filesize, long readedBytes)
{
int updated =Convert.ToInt32((readedBytes * 100) / Filesize);
progressBar1.Value = updated;
}
@ EDIT2
private void download()
{
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
res = (HttpWebResponse)req.GetResponse();
Int64 contentlength = res.ContentLength;
ResponseStr = res.GetResponseStream();
strLocal = new FileStream(@"E:\\xxx.tar.gz", FileMode.Create, FileAccess.Write, FileShare.None);
byte[]read=new byte[1024];
int readed = 0;
while ((readed = ResponseStr.Read(read, 0, read.Length)) > 0)
{
strLocal.Write(read, 0, readed);
backgroundWorker1.RunWorkerAsync();
// this.Invoke(new UpdateCallBack(this.UpdateProgress), new object[] {ResponseStr.Length,strLocal.Length });
}
strLocal.Close();
ResponseStr.Close();
}
private void UpdateProgress(long Filesize, long readedBytes)
{
int updated =Convert.ToInt32((readedBytes * 100) / Filesize);
progressBar1.Value = updated;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
UpdateProgress(ResponseStr.Length, strLocal.Length);
}
答案 0 :(得分:0)
您的所有操作都发生在同一个线程上。在进行下载时,应用程序会暂停UI的更新。我通常使用BackgroundWorker来更新这种情况的进度条。
Here's an MSDN resource on background workers.
根据您的编辑,以下是一些代码:
// At the class level, put this:
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true; // This means we want to send updates from the background worker.
// The DoWork method handles your time consuming task, which in this case is your download method. So you can try this:
private void Download()
{
bw.RunWorkerAsync();
}
// This should be called as a result:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
// The worker who's progress will be updated.
BackgroundWorker worker = sender as BackgroundWorker;
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
res = (HttpWebResponse)req.GetResponse();
Int64 contentLength = res.ContentLength;
ResponseStr = res.GetResponseStream();
strLocal = new FileStream(@"E:\xxx.tar.gz", FileMode.Create, FileAccess.Write, FileShare.None);
byte[] read = new byte[1024];
int readed = 0;
while((readed = ResponseStr.Read(read, 0, read.Length)) > 0)
{
strLocal.Write(read, 0, readed);
// Update the worker (this uses the number you calculated)
worker.ReportProgress(Convert.ToInt32((ResponseStr.Length * 100) / strLocal.Length));
strLocal.Close();
ResponseStr.Close();
}
}
// This is called via worker.ReportProgress
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
您需要在类中定义后台工作程序,并允许它接受进度更新。
然后,无论何时需要后台工作者的方法,都需要调用RunWorkerAsync
。您可以在下载方法中执行此操作,或者替换您调用下载的位置。
在你的工作班中,你会做你通常会做的所有事情,但是在你想要更新进度的情况下,你可以用必要的值来调用ReportProgress
。
如果您查看上面的链接,还可以实施其他方法 - 如果您担心取消后台工作人员或者是否有错误或其他任何方法。
修改强>
如果您仍然收到错误,我只是意识到您可能正在尝试从Stream
对象中提取长度。如果错误仍然存在,请查看this question。
答案 1 :(得分:0)
如上所述,它与线程有关。因此,您需要在多个线程上分离操作才能更新UI。您也可以使用async和await来解决此问题。下面是一篇很好的文章,文章是关于做一些动作和更新UI。
http://www.i-programmer.info/programming/c/1514-async-await-and-the-ui-problem.html