2012-01-26 12 views
0

私は、readメソッドを実行するために10秒ごとに実行されるWindowsサービスを持っています。 Readメソッドは、コンストラクタで提供される接続URLを使用してリモートサーバーに接続します。 リモートサーバーが応答しない場合、エラーをスローしてキャッチするようになります。スレッドを再び開始するにはどうしたらいいですか?Windowsサービスでスレッドを使用して非同期にC#メソッドを実行

class PMTicketsService 
{ 
    private Timer _serviceTimer; 
    private TimerCallback _timerDelegate; 

    public PMTicketsService() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnStart(string[] args) 
    { 
     // Set the method to execute when the timer executes. 
     _timerDelegate = new TimerCallback(Receive); 

     // Create timer and attach our method delegate to it 
     _serviceTimer = new Timer(_timerDelegate, null, 1000, 10000); 
    } 

    public void Receive(object state) 
    { 
     ABC abc = new ABC(Url); 
     ABC abc1 = new ABC(Url1); 

     /* Create the thread object, passing in the abc.Read() method 
     via a ThreadStart delegate. This does not start the thread. */ 
     Thread oThread = new Thread(new ThreadStart(abc.Read()); 
     Thread oThread1 = new Thread(new ThreadStart(abc1.Read()); 

     // Start the thread 
     oThread.Start(); 
     oThread1.Start(); 

     oThread.Join(); 
     oThread1.Join(); 
    } 
} 

class ABC 
{ 
    public string strUrl; 

    public ABC(string url) 
    { 
     strUrl = url; 
    } 

    public void Read() 
    { 
     try 
     { 
      // Code to use the connectionurl to access a remote server 
     } 
     catch (Exception ex) 
     { 
      // If the connection url fails to respond, how do we make thread start again? 
     } 
    } 
} 
+2

このコードでは、いくつかのクリーンアップを使用できます。 –

+0

@James。スレッドの例外をチェックして別のスレッドを開始するにはどうすればよいですか?コード例がありますか? – srikanth

+0

.NETのどのバージョンですか? –

答えて

1

将来、実際にコンパイルされるサンプルコードを提出する必要があります。私はあなたが持っていたものを取り出し、それをきれいにし、不要なタイマーを取り除き、必要なものを与えるはずの方法で構造化しました。次のコードでは、donetrueに設定するまで、Readメソッドは実行を継続します。

protected override void OnStart(string[] args) 
    { 
     try 
     { 
      ABC abc = new ABC("www.abc.com"); 

      // Create the thread object, passing in the abc.Read() method 
      Thread oThread = new Thread(new ThreadStart(abc.Read)); 

      // Start the thread 
      oThread.Start(); 
     } 
     catch (Exception) 
     { 

     } 
    } 

    public class ABC 
    { 
     string strUrl = ""; 

     public ABC(string url) 
     { 
      strUrl = url; 
     } 

     public void Read() 
     { 
      bool done = false; 

      while (!done) 
      { 
       try 
       { 
        //Code to use the connectionurl to access a remote server 
        //Use strUrl in here 
       } 
       catch (Exception) 
       { 
        //Wait 10 seconds before trying again 
        Thread.Sleep(10000); 
       } 

       //On success, set done to true 
       done = true; 
      } 
     } 
    } 
1

なぜ別のスレッドを開始しますか?スレッドの開始/停止は高価な操作であり、既存のスレッドを開いたままにして継続的に接続しようとする方がはるかに優れています。あなたはすでにスレッドがクラッシュしないようにtry/catchを持っています。ちょうどあなたが正常に接続したら、try/catchをしばらく(!done)ラップし、doneをtrueに設定してください。

Xを連続して接続できない場合(多分5?)、試行をやめたり、接続試行間のタイムアウトを長くしたりするようなコードを追加することもできます。

+0

コード例はありますか?スレッド化の初心者は – srikanth

+2

です。私はちょうどあなたがする必要があることをきちんと説明しました。あなたがする必要があるのはスレッド化とはまったく関係がありません。すべてのスレッドの処理はすでに完了しています。ブール値を定義し、whileループを追加し、おそらくThread.Sleepを追加する必要があります。それらはあなたが追加する必要があるコードの唯一の行です、そして、あなたがそれらを追加する必要があるところを教えました。私は完全に自信を持っています。ソースコードにコピーしたり、貼り付けたりする必要はありません。 – Servy

関連する問題