System.ApplicationException:DeleteTimerNative中的应用程序出错

本文关键字:应用 程序出错 DeleteTimerNative ApplicationException System | 更新日期: 2023-09-27 17:58:00

在我们的代码中,我们有一个计时器,它将检查服务器是否连接。如果未连接,将尝试重新连接服务器。

代码如下

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)

在经过的事件中,我们将停止计时器并更改间隔,然后再次启动计时器。这些步骤会引起任何异常吗?

System.ApplicationException:DeleteTimerNative中的应用程序出错

为什么要停止和启动计时器并设置间隔。您应该只检查服务器是否已连接。

更新

您正在停止计时器,这将删除事件内部的本机计时器。不是个好主意。你就不能不停不停地设定时间间隔吗。

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
}