HELP:我在线程中进行后台(WebClient网络下载)操作,可是前台的窗体还是阻塞(不能用鼠标移动等),而如果后台不是网络操作,而是其他的操作,UI 没有细看你的代码,不过没有发现sleep()之类的方法让线程停一下而处理其它事件,所以有可能会在界面上不响应.你可以在你的线程循环里加一个sleep(10)这样的方法来让你的程序界面响应. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正常来说你新建一个线程就不会有这种问题了啊。。sleep()只是延时,试试用Application.DoEvents();转移控制权 楼上的方法好像没效果啊 private void button1_Click(object sender, System.EventArgs e) { Thread t1 = new Thread( new ThreadStart(StartThreadNet));//这个线程启动网络下载,会让窗体不会刷新等. t1.Start(); }加上Sleep() 改为: private void button1_Click(object sender, System.EventArgs e) { Thread t1 = new Thread( new ThreadStart(StartThreadNet));//这个线程启动网络下载,会让窗体不会刷新等. t1.Start(); Thread.Sleep(10); }问题依然存在 TO:cnhgj(戏子)(黄某人养不成沙皮狗) 请问:Application.DoEvents();加在哪里呢? Thread t1 = new Thread( new ThreadStart(StartThreadNet)); Application.DoEvents();//如果加在这里 问题就更严重了. t1.Start(); Thread.Sleep(10); Application.DoEvents();只适用于Window应用程序,如果是Web的话可以就不行了,sleep()将当前线程阻塞指定的毫秒数,这样才有机会让其它的线程来执行啊,我也晕了,用代码试试去,汗..... 在启动线程时,调用Application.DoEvents(); private void button1_Click(object sender, System.EventArgs e) { Thread t1 = new Thread( new ThreadStart(StartThreadLoop));//这个线程启动网络下载,会让窗体不会刷新等. t1.Start(); } public void StartThreadLoop() { while(1==1) { WebClient wc = new WebClient(); try { byte[] response = wc.DownloadData("http://www.google.com/"); MessageBox.Show(Encoding.ASCII.GetString(response)); } catch { } } }做了一下测试。。结果不会像你所说的死掉。。 Application.DoEvents();你可以加在死循环中。。不过不加也没问题啊。因为你目前用的是多线程 Application.DoEvents()放进StartThreadNet中. 如下的代码如果把System.Threading.Thread.Sleep(1)这两句//了就出现楼主那样的情况,如果把//去掉了则没有了那样的情况了,可以试试先,如果要是在Window应用里则用Application.DoEvent()也会可以的:private void aa(){ int s = 0; for (int i = 0; i<100000000; i++) { //System.Threading.Thread.Sleep(1); s = i*i*i; this.textBox1.Text = s.ToString(); }}private void bb(){ int s = 0; for (int i = 0; i<100000000; i++) { //System.Threading.Thread.Sleep(1); s = i*i*i; this.textBox2.Text = s.ToString(); }}private void button1_Click(object sender, System.EventArgs e){ System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.aa)); t.Start(); System.Threading.Thread tt = new System.Threading.Thread(new System.Threading.ThreadStart(this.bb)); tt.Start();} 我觉得楼可应改下面的代码:public void StartThreadLoop(){ while(1==1) { System.Threading.Thread.Sleep(1); ; }} To: hbxtlhx(下着春雨的天)你的代码的线程里执行的是一半的运算,不加Sleep也不会有问题但是 如果线程代码里设计网络的话,就有问题了. To: cnhgj(戏子)(黄某人养不成沙皮狗)为什么把地址 http://www.cnandusa.com 换为 www.google.com 程序就没有问题了呢? To: ccnpwp不好意思,我还没用过QQ2004 http://www.cnandusa.com,这个网站应该是租用虚拟主机的,你不停提交的话,服务器会返回500错误。而你的代码中用了try{}catch{},所以你根本查觉不到 这个没办法怎么解决。。它本身服务器的性能就不怎么样。。就好像你有时访问CSDN会超时一样 刚才我看了服务器返回的具体错误,原是是用webclient不会有HTTP_USER_AGENT这个环境变量于是程序错误,造成500错误,而用IE就不会有问题了. 直接浏览器跟程序访问是有差别的,具体可以查看IIS日志 关于C# 编写的activeX错误,紧急求救! sql模糊查询问题 取值问题 pocket PC 的串口跟WIN32的串口程序同吗> 求各位大虾ASP.NET管理windows帐户的问题 如何用C#判断一个数的奇偶性? 类型在未被引用的程序集中定义? C#每个窗口之间通过什么进行回带和切换? conn.close和conn.dispose有什么区别 请教各位高手,如何判断一个窗体已经NEW了啊,是不是FormName==null? C#获取SQL存储过程参数问题 一个最最最简单的问题,而我又解决不了的问题,所以向各位请教.
private void button1_Click(object sender, System.EventArgs e)
{
Thread t1 = new Thread( new ThreadStart(StartThreadNet));//这个线程启动网络下载,会让窗体不会刷新等.
t1.Start();
}
加上Sleep() 改为:
private void button1_Click(object sender, System.EventArgs e)
{
Thread t1 = new Thread( new ThreadStart(StartThreadNet));//这个线程启动网络下载,会让窗体不会刷新等.
t1.Start();
Thread.Sleep(10);
}问题依然存在
请问:Application.DoEvents();
加在哪里呢?
Thread t1 = new Thread( new ThreadStart(StartThreadNet));
Application.DoEvents();//如果加在这里 问题就更严重了.
t1.Start();
Thread.Sleep(10);
{
Thread t1 = new Thread( new ThreadStart(StartThreadLoop));//这个线程启动网络下载,会让窗体不会刷新等.
t1.Start();
} public void StartThreadLoop()
{
while(1==1)
{
WebClient wc = new WebClient();
try
{
byte[] response = wc.DownloadData("http://www.google.com/"); MessageBox.Show(Encoding.ASCII.GetString(response));
}
catch
{
}
}
}做了一下测试。。结果不会像你所说的死掉。。
private void aa()
{
int s = 0;
for (int i = 0; i<100000000; i++)
{
//System.Threading.Thread.Sleep(1);
s = i*i*i;
this.textBox1.Text = s.ToString();
}
}
private void bb()
{
int s = 0;
for (int i = 0; i<100000000; i++)
{
//System.Threading.Thread.Sleep(1);
s = i*i*i;
this.textBox2.Text = s.ToString();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.aa));
t.Start();
System.Threading.Thread tt = new System.Threading.Thread(new System.Threading.ThreadStart(this.bb));
tt.Start();
}
public void StartThreadLoop()
{
while(1==1)
{
System.Threading.Thread.Sleep(1);
;
}
}
你的代码的线程里执行的是一半的运算,不加Sleep也不会有问题
但是 如果线程代码里设计网络的话,就有问题了.
为什么把地址 http://www.cnandusa.com 换为 www.google.com 程序就没有问题了呢?
不好意思,我还没用过QQ2004
于是程序错误,造成500错误,而用IE就不会有问题了.