私は、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?
}
}
}
このコードでは、いくつかのクリーンアップを使用できます。 –
@James。スレッドの例外をチェックして別のスレッドを開始するにはどうすればよいですか?コード例がありますか? – srikanth
.NETのどのバージョンですか? –