2011-02-28 62 views
1

我有一個Winform應用程序,它有一個backgroundWorker線程,並通過唯一的所有操作。REG線程:如何實現這

完成一個特定的操作,即連接到服務器後,我想創建一個監視器來檢查與服務器的連接是否存在。 Web服務將被執行以查明連通性並檢查結果。如果不重新連接它。我相信我應該創建一個用於監控連接的線程。邏輯如下:在每個X秒監視器檢查連通性之後,再次在X秒後返回,並且再次執行其工作,即檢查連接性並知道狀態。我不認爲他們有任何需要阻止線程,因爲每當我得到結果時我都可以做出反應。我也不需要處理這個過程中的任何UI。可能只需要一次,我發現連接丟失,然後必須調用其他重新連接的函數,並重新啓動這個線程或類似的東西。

我閱讀了2-3篇教程,但無法弄清楚如何實現這個 - 使用哪種線程類型和調用。

實現的代碼證:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){  
    if (currentState == OPENING) 
     LoadAfterLogin(); 
    else if (currentState == CONNECTING) 
     Connect(); // CONNECTS TO THE SERVER 
} 
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){ 
    ...... 
    else if (value == CONNECTED) { 
     currentState = CONNECTED; 
     statusLabel.Text = "CONNECTED Successfully to server"; 
     // HERE MONITOR THREAD SHOULD BE STARTED ONCE APP KNOWS FOR SURE, THAT IT IS CONNECTED TO SERVER  
     monitorConn = new MonitorConnection(); 
    } else if (value == EXP) { 
     currentState = ERR_CONNECTING; 
     ExceptionData ed = (ExceptionData)e.UserState; 
     ..... 
    } 
} 

////////////////////////////////////////ANOTHER MonitorConnection thread class 

public class MonitorConnection : Thread{ 
private DateTime startTime, lastCheckedTime; 
private bool conneced; 
private int intervalDuration, nextTime; 

public MonitorConnection() { 
    intervalDuration = 10000;  
} 
public bool IsConneced { 
    get { return conneced; } 
    set { conneced = value; } 
} 

// SHOULD CALL THIS WHEN THE THREAD IS STARTED 
public void start() { 
    startTime = DateTime.Now; 
    nextTime = DateTime.Now; 
} 

// THIS SHOULD BE SOMEHOW CALLED BY run() 
public void checkConnection() { 
    // IS CURRENT TIME IS >= SCHEDULED NEXTTIME 
    if (DateTime.Now >= nextTime) { 
     // Checks if the connection to server is alive or not 
     conneced = UltimateLibrary.http.HTTPUtility.isConnectionAvailable(); 
     lastCheckedTime = DateTime.Now; 
     nextTime = lastCheckedTime + intervalDuration; 
    } 
} 

// SOMETHING LIKE RUN - that starts the process execution 
public void run() { 
    while (true) { 
     if (DateTime.Now >= nextTime) 
      checkConnection(); 
     Thread.sleep(interval); 
    } 
} 

}

這裏已經implemetned表明,我要實現的邏輯的代碼。我同意並知道代碼並不完全符合其標記。仍然需要進行更改,以便通過不阻塞,以連續間隔運行等方式進行更改。我認爲除Thread之外的其他類應該擴展(不確定哪個是合適的)。

任何幫助,高度讚賞。任何人都可以請給我一些提示/想法來實現上述。

[編輯:] 感謝Vinay,根據你的代碼,我實現如下: - 在MonitorConnection類中創建一個Timer obj。

 
     public void StartMonitor() 
     { 
      startTime = DateTime.Now; 
      nextTime = DateTime.Now; 
      timer = new Timer(intervalDuration); 
      timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
      GC.KeepAlive(timer); // I don't think this is reqd as timer is a member of class 
      timer.Start(); // Can also call timer.Enabled = true; 
     }

public static void checkConnection() 
    { 
     if (DateTime.Now >= nextTime) { 
      // Checks if the connection to server is alive or not 
      connected = UltimateLibrary.http.HTTPUtility.isConnectionAvailable(); 
      lastCheckedTime = DateTime.Now; 
      nextTime = lastCheckedTime.AddMilliseconds(intervalDuration); 
     } 

    } 

    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     // Check if connection is alive 
     checkConnection(); 
    } 

。 如果連接是假的,即服務器沒有連接,那麼必須讓調用表單知道這一點。我想這可以傳遞一個形式的對象到這個類,並在連接== false時調用其各自的方法。你說什麼 ?這是否和上面的代碼似乎是正確的?您的指導是,並將不勝感激。 - 感謝

感謝

回答

0

爲什麼不使用Timer此 - 使用它很簡單。例如,

public event EventHandler Disconnected; 


    timer = new System.Timers.Timer(10000); 
    timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
    timer.Enabled = true; 

    ... 

    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     // check if connection is alive 
     conneced = UltimateLibrary.http.HTTPUtility.isConnectionAvailable(); 
     if (!connected) 
     { 
      // inform listeners 
      var listener = Disconnected; 
      if (null !- listener) 
      { 
       Disconnected(EventArgs.Empty); 
      } 
     } 
    } 

只關心你必須採取的措施,以確保定時器被引用所需的時間(否則它會被垃圾收集)。

+0

感謝VinayC,根據您的指導,我實施了代碼並添加了問題。它是否正確 !嘗試根據新的代碼和查詢來引導更多。 - 感謝 – Tvd 2011-02-28 13:14:21

+0

@ user455979,儘管你可以參考表單並調用它的方法,但它會採取不必要的依賴 - 我建議它從'MonitorConnection'中引發事件並在表單上處理它。 – VinayC 2011-03-01 04:02:25

+0

VinayC,你能幫助我知道如何在MonitorConnection中創建一個事件,並將其提升並在窗體中處理?任何教程或URL都會有幫助。我不認爲我有任何鏈接可以幫助我按照你所說的去做。 – Tvd 2011-03-01 14:39:37