你可以在timer前用if判斷網(wǎng)絡狀態(tài),
創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設計開發(fā)服務,多年建網(wǎng)站服務經(jīng)驗,各類網(wǎng)站都可以開發(fā),品牌網(wǎng)站制作,公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設計,建網(wǎng)站費用,建網(wǎng)站多少錢,價格優(yōu)惠,收費合理。
如斷開,可用
threading.Thread.Sleep(10000) ‘當前線程掛起10秒
’可以開一個新線程去讀取脫機數(shù)據(jù)。
如連接,則繼續(xù)執(zhí)行。
補充:
dim i as integer
'超過100次退出,避免死循環(huán)
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
多線程一般是不推薦用的,因為線程之間如果有共享資源的話會引起競爭,需要加鎖處理;而且線程間沒有時序關系,所以你在調(diào)試中可能會出現(xiàn)異步處理結(jié)束順序與開始處理順序不一致的情況(我在調(diào)試中已經(jīng)發(fā)現(xiàn)該問題)。
針對你提出的這個問題,采用了多線程處理,利用的是BackgroundWorker也就是異步處理控件進行了處理。
代碼已經(jīng)經(jīng)過調(diào)試通過。歡迎交流,如有問題,留下QQ或其他聯(lián)系方式。
代碼如下,并附程序截圖。
‘---------------------------------------------------
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后,按指定時間間隔規(guī)定向界面刷新,線程必須是全局變量
用一個有退出條件的無限循環(huán)的sub也行。
線程開始就觸發(fā)該過程,用無限循環(huán)來刷新狀態(tài),線程完成即退出無限循環(huán)。局部變量的線程也可以用這種方式。
不過用法要正確,否則會出問題。
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;
}
多線程里調(diào)用就行了 費了好長時間才弄出來 我調(diào)試過了 能行 如果對你有用就給分吧