2012-03-23 89 views
2

所以這是所有新的給我。所以,我希望獲得一些指導C#異步對象

假設我有一類類似下面

public class pinger 
{ 


    // Constructor 
    public Pinger() 
    { 
    do while exit = False; 
    Uri url = new Uri("www.abhisheksur.com"); 
     string pingurl = string.Format("{0}", url.Host); 
     string host = pingurl; 
     bool result = false; 
     Ping p = new Ping(); 
     try 
     { 
      PingReply reply = p.Send(host, 3000); 
      if (reply.Status == IPStatus.Success) 
       result = true; 
     } 
     catch { } 
     //wait 2 seconds 
     loop; 
    } 


} 

這樣我就可以調用此方法,

Pinger firstone = new Pinger 

我想如果用於控制再返回主線程離開創建的實例運行並每兩秒鐘ping主機並更新結果變量,然後當我想知道主線程的狀態時,我可以使用get屬性。

任何一個能提出一些良好的閱讀/例子來介紹我到多線程在C#中,使用ping作爲一個例子似乎是一個好容易的事情與:)

乾杯

嘗試了這一點

亞倫

回答

3

我可以勾勒出類應該是什麼樣子:-)

public class pinger 
{ 
    private Uri m_theUri; 
    private Thread m_pingThread; 
    private ManualResetEvent m_pingThreadShouldStop; 

    private volatile bool m_lastPingResult = false; 

    public Pinger(Uri theUri) 
    { 
     m_theUri = theUri; 
    } 


    public void Start() 
    { 
     if (m_pingThread == null) 
     { 
      m_pingThreadShouldStop = new ManualResetEvent(false); 
      m_pingThread = new Thread(new ParameterizedThreadStart(DoPing)); 
      m_pingThread.Start(m_theUri); 
     } 
    } 

    public void Stop() 
    { 
     if (m_pingThread != null) 
     { 
      m_pingThreadShouldStop.Set(); 
      m_pingThread.Join(); 

      m_pingThreadShouldStop.Close(); 
     } 
    } 


    public void DoPing(object state) 
    { 
     Uri myUri = state as Uri; 
     while (!m_pingThreadShouldStop.WaitOne(50)) 
     { 
      // Get the result for the ping 
      ... 

      // Set the property 
      m_lastPingResult = pingResult; 
     } 
    } 


    public bool LastPingResult 
    { 
     get { return m_lastPingResult; } 
    } 
} 

它有什麼作用?這是一個帶有StartStop方法的新類。 Start開始ping,Stop停止ping。

Ping是在單獨的線程中完成的,每次ping都會更新結果屬性。

+0

你們怎麼回事這麼快!我會稍微查看一下,讓你知道:)但這看起來很有希望。我可以理解編程的邏輯,只是希望我能夠學習和記住像你們一樣的語法! – DevilWAH 2012-03-23 10:42:19

+0

我不確定語法是否100%正確 - 當您將其粘貼到Visual Studio中時,您會看到:-)您做這類事情的時間越長,您將會記得如何做得越好。 – 2012-03-23 10:45:42

+0

ManualResetEvent,ParameterizedThreadStart,Thread,我得到「使用指令或程序集引用?」 我使用System.Threading。任務;包括在內,但還有什麼我失蹤? – DevilWAH 2012-03-23 11:27:39

3

我會爲此推薦任務並行庫(TPL)。關於使用TPL的一篇很好的文章可以在here找到。

有關在C#穿線其他信息來源可以在Joseph Albahari's blog找到。這應該提供您開始所需的所有信息。

我希望這會有所幫助。

編輯:如果你想的是如何將線穿入上面一個例子,我會很樂意提供幫助。

+0

我會看看他們奶酪,我會說是的一個例子的報價,但我認爲下面的Thorsten的有包起來。 :) – DevilWAH 2012-03-23 10:43:29

0

我想出了一個Task爲基礎的方法,其中有3條代碼路徑相互作用 - 請注意,最有可能的工作將只有一個線程來完成。

在下文進一步說明該程序的輸出是:

enter image description here

public class Program 
{ 
    public static object _o = new object(); 
    public static void Main(string[] args) 
    { 
     PingReply pingResult = null; 
     int i = 0; 
     Task.Run(async() => 
     { 
      var ii = 0; 
      //no error handling, example only 
      //no cancelling, example only 
      var ping = new System.Net.NetworkInformation.Ping(); 
      while (true) 
      { 
       Console.WriteLine($"A: {ii} > {DateTime.Now.TimeOfDay}"); 
       var localPingResult = await ping.SendPingAsync("duckduckgo.com"); 
       Console.WriteLine($"A: {ii} < {DateTime.Now.TimeOfDay}, status: {localPingResult?.Status}"); 
       lock (_o) 
       { 
        i = ii; 
        pingResult = localPingResult; 
       } 

       await Task.Delay(1000); 
       ii++; 
      } 
     }); 

     Task.Run(async() => 
     { 
      //no error handling, example only 
      while (true) 
      { 
       await Task.Delay(2000); 
       lock (_o) 
       { 
        Console.WriteLine($"B: Checking at {DateTime.Now.TimeOfDay}, status no {i}: {pingResult?.Status}"); 
       } 
      } 
     }); 

     Console.WriteLine("This is the end of Main()"); 
     Console.ReadLine(); 
    } 
}