0

コードには、サーバーが接続されているかどうかを確認するタイマーがあります。接続されていない場合は、サーバーへの再接続を試みます。

コードは以下の通りです

private void timer_Elapsed(object source, ElapsedEventArgs e)
{
   timer.Stop();
   if (timer.Interval != serverTimeOut)
   {
     timer.Interval = serverTimeOut;
   }

   timer.Start();
   //below the code to check whether server connected or not

}

次のような例外が発生しています

Class: System.Threading.TimerBase
Function: DeleteTimerNative
Exception: System.ApplicationException: Error in the application.
   at System.Threading.TimerBase.DeleteTimerNative(SafeHandle notifyObject)
   at System.Threading.TimerBase.Dispose()
   at System.Threading.Timer.Dispose()
   at System.Timers.Timer.set_Enabled(Boolean value)
   at System.Timers.Timer.Stop()
   at ServerConnection.timer_Elapsed(Object source, ElapsedEventArgs e)

経過イベントでは、タイマーを停止して間隔を変更し、再びタイマーを開始します。この手順で例外が発生しますか?

4

1 に答える 1

0

タイマーを停止して開始し、間隔を設定するのはなぜですか。サーバーが接続されているかどうかを確認するだけです。

アップデート

タイマーを停止すると、イベント内のネイティブ タイマーが削除されます。素晴らしいアイデアではありません。停止して開始せずに間隔を設定することはできませんか。

private void timer_Elapsed(object source, ElapsedEventArgs e)
{
   if (timer.Interval != serverTimeOut)
     timer.Interval = serverTimeOut;

   //below the code to check whether server connected or not

}
于 2013-02-07T05:54:20.610 に答える