解决方案 »
- 急救啊,事件传值问题
- [向computerfox提问] 函数参数,方法意思
- MID中,怎样用代码指定那个子窗是活动窗呢?
- 请问在.net2005下水晶报表怎么实现 批量邦定数据?? 我想把dataset的每行数据循环邦定到报表上 急。。。在线等
- C#端口扫描器
- 如何监视并控制AxWebBrowser下载网页中的图片
- 调用excel的时候报:接口 Excel._Application 的 QueryInterface 失败!急!不够加分!
- winform窗口问题?
- IE中的iframe中嵌入ActiveX控件闪烁问题,跪求。
- web下有没有象messagebox之类的函数 来弹出一个报错窗口
- 求助关于C#连接oracle数据的问题
- |ZYCWPF| WebBrowser 在什么时候对img标签进行拦截显示,有详细说明
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个方法
一直在这句话报错
单个手动执行button按钮是可以的
但是写循环 分步执行button 报错了
流不支持io并发读或写操作
new StreamReader(netStream)
这样new一下就可以读写了
我有点迷茫
netStream这个对象不要直接用,new StreamReader(netStream)这样把它放到一个新的StreamReader对象里,用这个新对象读数据,放在while外面