2012-03-23 27 views
2

私は実際にはasynの操作の前や複数のスレッドでプレイしていません。だから私はいくつかのガイダンスを期待していたc#async object

私はので、私は、その後の制御のために戻った場合、私が欲しいもの

Pinger firstone = new Pinger 

でこれを呼び出すことができます

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; 
    } 


} 

以下のようなクラスのものを持っていると仮定メインスレッドは、作成されたインスタンスを実行して2秒ごとにpingを実行し、結果変数を更新してから、メインスレッドからステータスを知りたいときにgetプロパティを使用できます。

いずれかの例としてのPingを使用して、C#でマルチスレッドに私を紹介するいくつかの良い読書/例を提案することができますが:)

乾杯

アーロン

でこれを試してみるには良い簡単なことに見えました

答えて

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

+0

構文が100%正しいかどうかはわかりません。これをVisual Studioに貼り付けると表示されます:-)そのようなことを長く行うほど、操作方法が覚えやすくなります。 –

+0

ManualResetEvent、ParameterizedThreadStart、Thread、 "usingディレクティブまたはアセンブリ参照を取得しますか?" 私はSystem.Threadingを使用しています。タスク。含まれていますが、私は行方不明のいくつかのものがありますか? – DevilWAH

3

私はこのためにタスク並列ライブラリ(TPL)をお勧めします。 TPLの使用に関する素晴らしい記事はhereです。 C#でスレッド上の他のINFOMATIONについて

Joseph Albahari's blogで見つけることができます。これは、あなたが始めるために必要なすべての情報を提供するはずです。

こちらがお役に立てば幸いです。

編集:上記をスレッドする方法の例が必要な場合は、お手伝いします。

+0

私はチーズをそれらを見ていきます、私は例の提供にyesと言うだろうが、私はトルステンの下には、ということがあると思います包まれた。 :) – DevilWAH

0

私は、相互作用する3つのコードパスがあるTaskベースのアプローチを思い付いた - 最も可能性の高い仕事はただ一つのスレッドで行われることに注意してください。

さらに以下に示すプログラムの出力は次のとおりです。

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(); 
    } 
} 
関連する問題