Modules/EDMI_ClientSuite/ClassTimer.vb

67 lines
2.1 KiB
VB.net

Imports System.ServiceModel
Imports System.Threading
Imports System.Timers
Imports DigitalData.Modules.EDMIFileOps.EDMIServiceReference
Imports DigitalData.Modules.Logging
Public Class ClassTimer
Private _Callback As TimerCallback
Private _LogConfig As LogConfig
Private _Logger As Logger
Private _Timer As Timers.Timer
Private _Channel As IEDMServiceChannel
Public Event OnlineChanged As OnlineChangedEventHandler
Public Delegate Sub OnlineChangedEventHandler(sender As Object, Online As Boolean)
Public Sub New(LogConfig As LogConfig, SynchronizingObject As frmMain, HeartbeatInterval As Integer)
Try
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_Channel = My.ChannelFactory.CreateChannel()
_Channel.Open()
_Timer = New Timers.Timer(HeartbeatInterval) With {
.SynchronizingObject = SynchronizingObject,
.Enabled = True
}
AddHandler _Timer.Elapsed, AddressOf Callback
Catch ex As Exception
_Logger.Error(ex)
End Try
End Sub
Public Async Sub Callback(sender As Object, e As ElapsedEventArgs)
Try
_Logger.Debug("Checking if Service is up...")
If _Channel.State = ServiceModel.CommunicationState.Faulted Then
_Channel = My.ChannelFactory.CreateChannel()
End If
' Connect to service and send hearbeat request
Dim oResult = Await _Channel.HeartbeatAsync()
_Logger.Debug("Service is online")
SetOnlineState(True)
Catch ex As Exception
_Logger.Debug("Service is offline!")
SetOnlineState(False)
Finally
My.Application.Service.LastChecked = DateTime.Now
End Try
End Sub
Private Sub SetOnlineState(NewState As Boolean)
If My.Application.Service.Online <> NewState Then
My.Application.Service.Online = NewState
RaiseEvent OnlineChanged(Me, NewState)
End If
End Sub
End Class