2013-08-24 70 views
0

我在c#中有一個測試web連接表單。我想在連接被檢查時顯示加載窗口,然後顯示檢查結果。如何創建加載窗口,而Web連接正在檢查Windows窗體應用程序?

這是我測試的網絡連接代碼:

public bool ConnectionAvailable(string strServer) 
    { 
     try 
     { 
      HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer); 

      HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse(); 
      if (HttpStatusCode.OK == rspFP.StatusCode) 
      { 
       // HTTP = 200 - Internet connection available, server online 
       rspFP.Close(); 
       return true; 
      } 
      else 
      { 
       // Other status - Server or connection not available 
       rspFP.Close(); 
       return false; 
      } 
     } 
     catch (WebException) 
     { 
      // Exception - connection not available 
      return false; 
     } 
    } 

這:

private void button1_Click(object sender, EventArgs e) 
    { 
     string url = "Web-url"; 
     label1.Text = "Checking ..."; 
     button1.Enabled = false; 

     if (ConnectionAvailable(url)) 
     { 
      WebClient w = new WebClient(); 
      w.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 
      label1.Text = w.UploadString(url, "post", "SN=" + textBox1.Text); 
      button1.Enabled = true; 
     } 
     else 
     { 
      label1.Text = "Conntion fail"; 
      button1.Enabled = true; 
     } 

    } 

回答

0

在Windows窗體應用程序的用戶界面在一個線程中運行,如果嘗試運行一個長期運行的進程,它檢查網絡連接可能會最終成爲這將導致形式凍結,直到它完成的工作。

於是,我開始一個新的線程,做檢查。然後提出事件以返回結果。儘管發生了這些情況,您可以使用用戶界面進行操作,如加載圖形,甚至允許用戶繼續使用不需要互聯網連接的功能。

創建EventArgs類你自己的,所以你可以傳回的結果是:在你的窗體類

public class ConnectionResultEventArgs : EventArgs 
{ 
    public bool Available { get; set; } 
} 

然後,創建你的事件,處理程序和方法來操作,當事件到達

//Create Event and Handler 
    public delegate void ConnectionResultEventHandler(object sender, ConnectionResultEventArgs e); 
    public event ConnectionResultEventHandler ConnectionResultEvent; 

//Method to run when the event has been receieved, include a delegate in case you try to interact with the UI thread 
    delegate void ConnectionResultDelegate(object sender, ConnectionResultEventArgs e); 
    void ConnectionResultReceived(object sender, ConnectionResultEventArgs e) 
    { 
     //Check if the request has come from a seperate thread, if so this will raise an exception unless you invoke. 
     if (InvokeRequired) 
     { 
      BeginInvoke(new ConnectionResultDelegate(ConnectionResultReceived), new object[] { this, e }); 
      return; 
     } 

     //Do Stuff 
     if (e.Available) 
     { 
      label1.Text = "Connection Good!"; 
      return; 
     } 

     label1.Text = "Connection Bad"; 
    } 

訂閱事件,當您加載窗體:

private void Form1_Load(object sender, EventArgs e) 
    { 
     //Subscribe to the the results event. 
     ConnectionResultEvent += ConnectionResultReceived; 
    } 

,然後安裝在W orker thread:

//Check the connection 
    void BeginCheck() 
    { 
     try 
     { 
      HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create("http://google.co.uk"); 

      HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse(); 
      if (HttpStatusCode.OK == rspFP.StatusCode) 
      { 
       // HTTP = 200 - Internet connection available, server online 
       rspFP.Close(); 

       ConnectionResultEvent(this, new ConnectionResultEventArgs {Available = true}); 
      } 
      else 
      { 
       // Other status - Server or connection not available 
       rspFP.Close(); 

       ConnectionResultEvent(this, new ConnectionResultEventArgs { Available = false }); 
      } 
     } 
     catch (WebException) 
     { 

      // Exception - connection not available 
      //Raise the Event - Connection False 
      ConnectionResultEvent(this, new ConnectionResultEventArgs { Available = false }); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //loading graphic, screen or whatever 
     label1.Text = "Checking Connection..."; 

     //Begin the checks - Start this in a new thread 
     Thread t = new Thread(BeginCheck); 
     t.Start(); 
    } 
+0

你需要使用這個:using System.Threading;感謝... – SmithMart

+0

...它的工作很好.... –

0

我想穿的!一個線程檢查連接,而另一個線程顯示加載窗口。例如,如果連接已建立,您可以通知另一個線程並顯示結果。

+0

準確地說,我想要這個....怎麼樣? –

相關問題