问题描述
大家好,我在做个抓取网页的小工具,网页打开速度慢的时候界面就会卡主无响应(如下图),我已经使用了Threading还是不行,请大神帮忙看看哈。代码如下usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingHttpHelps;usingSystem.Threading;namespaceSportLottertPost{publicpartialclassMain:Form{publicdelegatevoidDelegateDoWork();publicdelegatevoidDelegateReceiveData();Threadtr;publicMain(){InitializeComponent();}//程序启动激活定时器privatevoidMain_Load(objectsender,EventArgse){timer1.Interval=1000*10;timer1.Enabled=true;}//定时器启动线程privatevoidtimer1_Tick(objectsender,EventArgse){tr=newThread(ReceiveData);tr.IsBackground=true;tr.Start();}//接收数据privatevoidReceiveData(){if(this.InvokeRequired){this.BeginInvoke(newDelegateReceiveData(ReceiveData));}else{//WebClinet获取网站信息--------------------------------------------stringurl="http://192.168.1.66test.aspx";HttpPosthttppost=newHttpPost(url);string[,]urlpam={{"MsgType","01"},{"MsgNo","012345678"}};stringjsonstr=httppost.SendPost(urlpam);//-------------------------------------------------------------------if(jsonstr==""){jsonstr="无数据";}listBox1.Items.Add(jsonstr);}tr.Abort();}}}
解决方案
本帖最后由 huangzhiqiang800 于 2015-06-17 11:13:10 编辑
解决方案二:
每10s都创建一个thread,,这,,,,privatevoidReceiveData(){if(this.InvokeRequired){this.BeginInvoke(newDelegateReceiveData(ReceiveData));}}这个是写法??
解决方案三:
不知道怎么写啊,该怎么写呢
解决方案四:
用timer创建线程
解决方案五:
引用3楼Imcx_的回复:
用timer创建线程
楼主直接把抓取方法放到timer里,激活timer就好了啊
解决方案六:
http://192.168.1.66test.aspx不是这样么?http://192.168.1.66/test.aspx
解决方案七:
为什么创建一个timer,还要用timer去创建线程tr......
解决方案八:
你的timer就相当于一个线程了,创建tr多余