jj: ZUGFeRD
This commit is contained in:
84
DDZUGFeRDService/ThreadRunner.vb
Normal file
84
DDZUGFeRDService/ThreadRunner.vb
Normal file
@@ -0,0 +1,84 @@
|
||||
Imports System.ComponentModel
|
||||
Imports System.IO
|
||||
Imports System.Timers
|
||||
Imports DigitalData.Modules.Interfaces
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public Class ThreadRunner
|
||||
|
||||
Private WithEvents _workerThread As BackgroundWorker
|
||||
Private WithEvents _workerTimer As Timer
|
||||
|
||||
Private _logConfig As LogConfig
|
||||
Private _logger As Logger
|
||||
Private _directories As List(Of String)
|
||||
|
||||
Private Const TIMER_INTERVAL = 60_000
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, Directories As List(Of String))
|
||||
_logConfig = LogConfig
|
||||
_logger = _logConfig.GetLogger()
|
||||
_directories = Directories
|
||||
|
||||
_workerThread = New BackgroundWorker() With {
|
||||
.WorkerReportsProgress = True,
|
||||
.WorkerSupportsCancellation = True
|
||||
}
|
||||
|
||||
_workerTimer = New Timer With {
|
||||
.Interval = TIMER_INTERVAL
|
||||
}
|
||||
End Sub
|
||||
|
||||
Public Sub Start()
|
||||
_workerTimer.Start()
|
||||
_logger.Info("ThreadRunner started.")
|
||||
End Sub
|
||||
|
||||
Public Sub [Stop]()
|
||||
If _workerThread.IsBusy Then
|
||||
_workerThread.CancelAsync()
|
||||
_logger.Info("Worker cancelled.")
|
||||
End If
|
||||
_workerTimer.Stop()
|
||||
_logger.Info("ThreadRunner stopped.")
|
||||
End Sub
|
||||
|
||||
Private Sub TimerElapsed(sender As Object, e As ElapsedEventArgs) Handles _workerTimer.Elapsed
|
||||
If Not _workerThread.IsBusy Then
|
||||
_workerThread.RunWorkerAsync(_directories)
|
||||
Else
|
||||
_logger.Warn("Worker is busy")
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork
|
||||
Dim oDirectories As List(Of String) = e.Argument
|
||||
|
||||
For Each oPath As String In oDirectories
|
||||
Dim oDirInfo As New DirectoryInfo(oPath)
|
||||
|
||||
_logger.Info($"Processing directory {oDirInfo.FullName}..")
|
||||
|
||||
If oDirInfo.Exists Then
|
||||
Dim oFiles As List(Of FileInfo) = oDirInfo.GetFiles().ToList()
|
||||
Dim oFileCount = oFiles.Count
|
||||
Dim oCurrentFileCount = 0
|
||||
|
||||
For Each oFile In oFiles
|
||||
oCurrentFileCount += 1
|
||||
_logger.Info($"Processing file {oFile.FullName} (${oCurrentFileCount}/{oFileCount})")
|
||||
|
||||
ZUGFeRDInterface.ExtractXMLFile(oFile.FullName)
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
End Sub
|
||||
Private Sub WorkProgress(sender As Object, e As ProgressChangedEventArgs) Handles _workerThread.ProgressChanged
|
||||
Throw New NotImplementedException()
|
||||
End Sub
|
||||
|
||||
Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted
|
||||
Throw New NotImplementedException()
|
||||
End Sub
|
||||
End Class
|
||||
Reference in New Issue
Block a user