十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你可以在timer前用if判断网络状态,
创新互联公司专注于网站建设,为客户提供成都做网站、成都网站制作、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,品牌网站制作,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。
如断开,可用
threading.Thread.Sleep(10000) ‘当前线程挂起10秒
’可以开一个新线程去读取脱机数据。
如连接,则继续执行。
补充:
dim i as integer
'超过100次退出,避免死循环
for i=0 to 100
try
'ping你的端口
if ‘ok
exit for
else
threading.Thread.Sleep(10000) ‘当前线程挂起10秒
end
Catch ex As Exception
End Try
next
多线程一般是不推荐用的,因为线程之间如果有共享资源的话会引起竞争,需要加锁处理;而且线程间没有时序关系,所以你在调试中可能会出现异步处理结束顺序与开始处理顺序不一致的情况(我在调试中已经发现该问题)。
针对你提出的这个问题,采用了多线程处理,利用的是BackgroundWorker也就是异步处理控件进行了处理。
代码已经经过调试通过。欢迎交流,如有问题,留下QQ或其他联系方式。
代码如下,并附程序截图。
‘---------------------------------------------------
Imports System.ComponentModel '导入异步控件命名空间
Public Class Form1
Private howmany As Integer = 10
Private AnalysisNumber(0 To howmany - 1) As BackgroundWorker
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
creatNewBackgroundWorker()
addHandle()
startWork()
End Sub
Private Sub creatNewBackgroundWorker()
For i As Integer = 0 To AnalysisNumber.Length - 1
AnalysisNumber(i) = New BackgroundWorker
Next
End Sub
Private Sub addHandle()
For i As Integer = 0 To AnalysisNumber.Length - 1
AddHandler AnalysisNumber(i).DoWork, AddressOf AnalysisNumber_DoWork
AddHandler AnalysisNumber(i).RunWorkerCompleted, AddressOf AnalysisNumber_RunWorkerCompleted
Next
End Sub
Private Sub startWork()
For i As Integer = 0 To 9
Dim temp(0 To 9) As Integer
For j As Integer = 1 To 10
temp(j - 1) = 10 * i + j
Next
AnalysisNumber(i).RunWorkerAsync(temp)
Next
End Sub
Private Sub AnalysisNumber_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim data As Integer()
data = CType(e.Argument, Integer())
Dim temp As Integer
For i As Integer = 0 To data.Length - 1
temp = data(i)
data(i) = temp * temp
Next
e.Result = data
End Sub
Private Sub AnalysisNumber_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim data As Integer()
data = CType(e.Result, Integer())
For i As Integer = 0 To data.Length - 1
ListBox1.Items.Add(data(i))
Next
End Sub
End Class
用time控件啊,获取threadstate后,按指定时间间隔规定向界面刷新,线程必须是全局变量
用一个有退出条件的无限循环的sub也行。
线程开始就触发该过程,用无限循环来刷新状态,线程完成即退出无限循环。局部变量的线程也可以用这种方式。
不过用法要正确,否则会出问题。
public static string getXmlFile(String url, String paramList,string referer)
{
HttpWebResponse res = null;
string strResult = " ";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET ";
req.KeepAlive = true;
req.Referer=referer;
//CookieContainer cookieCon = new CookieContainer();
//req.CookieContainer = cookieCon;
//req.CookieContainer.SetCookies(new Uri(url),cookieheader);
StringBuilder UrlEncoded = new StringBuilder();
res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding( "gb2312 ");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
strResult = e.ToString();
}
finally
{
if ( res != null )
{
res.Close();
}
}
return strResult;
}
多线程里调用就行了 费了好长时间才弄出来 我调试过了 能行 如果对你有用就给分吧