紧急求助 c# 流不支持并发IO读写问题

ikemoto2011 2012-12-11 03:23:07
private void downloadFile1()
{
int bufferSize;
DelegateUpdateData UIDel = new DelegateUpdateData(updateUI);

netStream = httpResponse.GetResponseStream();
while ((bufferSize = netStream.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0)
{
curDownSize += bufferSize;
this.Invoke(UIDel, curDownSize);
fileStream.Write(downloadBuffer, 0, bufferSize);
}

netStream.Close();
fileStream.Close();


}


我 循环读取3次
一直在这句话 报错
提示 流不支持IO并发读或者写
求助啊


我就是写一个测试软件的小工具 每个ip地址下面 有3个链接
通过循环去测速
我手动设置3个button 分别点击 button 不会报错
但是写循环时出错
...全文
778 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
stdorado 2013-06-14
  • 打赏
  • 举报
回复
filemode正确吗?
oriency755 2012-12-13
  • 打赏
  • 举报
回复
引用 7 楼 ikemoto2011 的回复:
引用 6 楼 oriency755 的回复:http流不支持读写 C# code?1new StreamReader(netStream) 这样new一下就可以读写了 麻烦说下 在哪个地方new 下 我有点迷茫
netStream这个对象不要直接用,new StreamReader(netStream)这样把它放到一个新的StreamReader对象里,用这个新对象读数据,放在while外面
ikemoto2011 2012-12-12
  • 打赏
  • 举报
回复
引用 6 楼 oriency755 的回复:
http流不支持读写 C# code?1new StreamReader(netStream) 这样new一下就可以读写了
麻烦说下 在哪个地方new 下 我有点迷茫
oriency755 2012-12-11
  • 打赏
  • 举报
回复
http流不支持读写
new StreamReader(netStream)
这样new一下就可以读写了
ikemoto2011 2012-12-11
  • 打赏
  • 举报
回复
3楼说的 我也试过了 我不是设置成 读写模式的嘛
ikemoto2011 2012-12-11
  • 打赏
  • 举报
回复
while ((bufferSize = netStream.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0) 一直在这句话报错 单个手动执行button按钮是可以的 但是写循环 分步执行button 报错了 流不支持io并发读或写操作
niss 2012-12-11
  • 打赏
  • 举报
回复
看看是不是FileMode和FileAccess没有设置正确?
ikemoto2011 2012-12-11
  • 打赏
  • 举报
回复
void updateUI(int downSize) { pbDown.Value = downSize; if (downSize == pbDown.Maximum) { timer1.Enabled = false; lbSpeed.Text = ((pbDown.Maximum / 1024) / (DateTime.Now.Subtract(timeStart).Seconds)) + "KB/S"; lbtime.Text = DateTime.Now.Subtract(timeStart).Seconds.ToString () + "s"; lbsize.Text = (downSize/1024/1024).ToString ()+"M"; lbInfo.Text = "下载完毕"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string file = filePath + DateTime.Now.ToLongDateString() + ".txt"; string content ="\r\n"+ "下载地址:" + tbFile.Text.ToString() + "\r\n" + "下载平均速度:" + lbSpeed.Text.ToString() + "\r\n" + "下载所需时间:" + lbtime.Text.ToString() +"\r\n"+ "文件大小:" + lbsize.Text.ToString() ; } 这段是 updateUI 方法 然后 再 public int returnstr(string tbfilestr) { int num = 0; if (string.IsNullOrEmpty(tbfilestr)) { MessageBox.Show("请输入需要下载的文件."); return 0 ; } timer1.Interval = 100; lbInfo.Text = "下载文件中..."; // btnDownload.Enabled = false; Application.DoEvents(); // 刷新界面 curDownSize = 0; lastDownSize = 0; /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时) timer1.Enabled = true; try { WebRequest httpRequest = WebRequest.Create(tbfilestr); httpResponse = httpRequest.GetResponse(); pbDown.Maximum = (int)httpResponse.ContentLength; } catch (Exception _ex) { MessageBox.Show(_ex.Message); lbInfo.Text = "点击按钮开始下载"; // button2.Enabled = true; return 0; } timeStart = DateTime.Now; object locker = new object(); //lock (locker) //{ /// 开启文件本地保存流 /// using (System.IO.StreamWriter sw = System.IO.File.AppendText(logFile)) fileStream = new System.IO.FileStream(tbfilestr.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite); /// 开启下载进程 downloadThread = new Thread(new ThreadStart(downloadFile)); downloadThread.IsBackground = true; downloadThread.Start(); //} num = 1; return num; } private void button5_Click(object sender, EventArgs e) { //returnstr(tbFile.Text.ToString()); //if (label5.Text == "地址1下载完成") //{ // returnstr(tbFile2.Text.ToString()); //} //returnstr(tbFile.Text.ToString()); } private void btnDownload2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbFile2.Text.Trim())) { MessageBox.Show("请输入需要下载的文件."); return; } timer1.Interval = 100; lbInfo.Text = "下载文件中..."; // btnDownload2.Enabled = false; Application.DoEvents(); // 刷新界面 curDownSize = 0; lastDownSize = 0; /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时) timer1.Enabled = true; try { WebRequest httpRequest = WebRequest.Create(tbFile2.Text.Trim()); httpResponse = httpRequest.GetResponse(); pbDown.Maximum = (int)httpResponse.ContentLength; } catch (Exception _ex) { MessageBox.Show(_ex.Message); lbInfo.Text = "点击按钮开始下载"; btnDownload2.Enabled = true; return; } timeStart = DateTime.Now; object locker = new object(); lock (locker) { /// 开启文件本地保存流 fileStream = new System.IO.FileStream(tbFile2.Text.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite ); /// 开启下载进程 downloadThread = new Thread(new ThreadStart(downloadFile2)); downloadThread.IsBackground = true; downloadThread.Start(); } } private void btnDownload3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbFile3.Text.Trim())) { MessageBox.Show("请输入需要下载的文件."); return; } timer1.Interval = 100; lbInfo.Text = "下载文件中..."; // btnDownload3.Enabled = false; Application.DoEvents(); // 刷新界面 curDownSize = 0; lastDownSize = 0; /// 开启界面刷新计时器 (不使用线程,防止阻塞造成界面刷新不及时) timer1.Enabled = true; try { WebRequest httpRequest = WebRequest.Create(tbFile3.Text.Trim()); httpResponse = httpRequest.GetResponse(); pbDown.Maximum = (int)httpResponse.ContentLength; } catch (Exception _ex) { MessageBox.Show(_ex.Message); lbInfo.Text = "点击按钮开始下载"; btnDownload.Enabled = true; return; } timeStart = DateTime.Now; object locker = new object(); lock (locker) { /// 开启文件本地保存流 fileStream = new System.IO.FileStream(tbFile3.Text.Split('/').Last<string>(), System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite); /// 开启下载进程 downloadThread = new Thread(new ThreadStart(downloadFile3)); downloadThread.IsBackground = true; downloadThread.Start(); } } 调用上面 2个方法
viki117 2012-12-11
  • 打赏
  • 举报
回复
看不懂,代码好含蓄

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧