Imports System.ComponentModel Imports System.Timers Imports DigitalData.Modules.Database Imports DigitalData.Modules.Jobs Imports DigitalData.Modules.Logging Public Class JobRunner Private WithEvents _workerThread As BackgroundWorker Private WithEvents _workerTimer As Timer Private _interval As Long Private _logConfig As LogConfig Private _logger As Logger Private _firebird As Firebird Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Interval As Long) _logConfig = LogConfig _logger = _logConfig.GetLogger() _firebird = Firebird _interval = Interval _workerTimer = New Timer() _workerThread = New BackgroundWorker() With { .WorkerReportsProgress = False, .WorkerSupportsCancellation = True } End Sub Public Sub Start() _workerTimer.Interval = _interval * 1000 _workerTimer.Start() _logger.Debug("JobRunner started with {0}s Interval.", _interval) End Sub Public Sub [Stop]() Try If _workerThread.IsBusy Then _workerThread.CancelAsync() _logger.Debug("Background Worker cancelled.") End If _workerTimer.Stop() _logger.Debug("Background Worker stopped.") Catch ex As Exception _logger.Error(ex) End Try End Sub Private Sub TimerElapsed(sender As Object, e As ElapsedEventArgs) Handles _workerTimer.Elapsed If Not _workerThread.IsBusy Then _workerThread.RunWorkerAsync() Else _logger.Warn("Background Worker is busy") End If End Sub Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork Try Dim args As WorkerArgs = e.Argument _logger.Debug("Background worker running..") ' TODO: WORK Dim oJob As New ADSyncJob(_logConfig, _firebird) Dim oArgs As New ADSyncArgs() With { .Enabled = My.Settings.JOB_ADSYNC_ENABLED, .Interval = My.Settings.JOB_ADSYNC_INTERVAL, .RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH } If oJob.ShouldStart(oArgs) Then oJob.Start(oArgs) End If Catch ex As Exception _logger.Warn("Background worker failed!") _logger.Error(ex) End Try End Sub Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted _logger.Debug("Background worker completed!") End Sub End Class