Common: improve file watcher, fix refreshing data after update

This commit is contained in:
Jonathan Jenne
2022-02-08 16:28:24 +01:00
parent 533df59b1f
commit c6e67a967c
16 changed files with 221 additions and 65 deletions

View File

@@ -35,6 +35,7 @@ Namespace DocumentResultList
Public ProcessId As Integer
Public FilePath As String
Public CurrentlyProcessing As Boolean = False
Public Exited As Boolean = False
End Class
Public Class FileChangedArgs
@@ -51,11 +52,11 @@ Namespace DocumentResultList
End Sub
Public Async Function OpenDocument(pDocument As Document) As Task(Of Boolean)
Dim oResult As Tuple(Of Process, String) = Nothing
Dim oResult As Tuple(Of Integer, String) = Nothing
If pDocument.FullPath IsNot Nothing AndAlso pDocument.FullPath.Trim <> String.Empty Then
' TODO: DONT put into openfiles
oResult = OpenFileFromPath(pDocument)
oResult = Await OpenFileFromPath(pDocument)
ElseIf pDocument.Extension IsNot Nothing AndAlso pDocument.Contents IsNot Nothing Then
oResult = Await OpenFileFromByteArray(pDocument)
@@ -67,15 +68,15 @@ Namespace DocumentResultList
Return False
End If
Dim oProcess = oResult.Item1
Dim oProcessId = oResult.Item1
Dim oFilePath = oResult.Item2
Logger.Debug("File [{0}] opened with ProcessId [{1}]", oFilePath, oProcess.Id)
Logger.Debug("File [{0}] opened with ProcessId [{1}]", oFilePath, oProcessId)
Dim oOpenFile = New OpenFile With {
.Document = pDocument,
.FilePath = oFilePath,
.ProcessId = oProcess.Id
.ProcessId = oProcessId
}
OpenFiles.Add(oOpenFile)
@@ -97,7 +98,7 @@ Namespace DocumentResultList
ProcessedFiles.Remove(pOpenFile)
End Sub
Private Async Function OpenFileFromByteArray(pDocument As Document) As Task(Of Tuple(Of Process, String))
Private Async Function OpenFileFromByteArray(pDocument As Document) As Task(Of Tuple(Of Integer, String))
Try
Dim oTempPath = Path.Combine(Path.GetTempPath(), Constants.TEMP_PATH_SUBFOLDER)
Dim oDirectory = Directory.CreateDirectory(oTempPath)
@@ -110,8 +111,9 @@ Namespace DocumentResultList
End Using
End Using
Dim oProcess = DoOpenFile(oFilePath)
Return New Tuple(Of Process, String)(oProcess, oFilePath)
Dim oProcessId = Await DoOpenFile(oFilePath)
Return New Tuple(Of Integer, String)(oProcessId, oFilePath)
Catch ex As Exception
Logger.Error(ex)
@@ -120,10 +122,10 @@ Namespace DocumentResultList
End Try
End Function
Private Function OpenFileFromPath(pDocument As Document) As Tuple(Of Process, String)
Private Async Function OpenFileFromPath(pDocument As Document) As Task(Of Tuple(Of Integer, String))
Try
Dim oProcess = DoOpenFile(pDocument.FullPath)
Return New Tuple(Of Process, String)(oProcess, pDocument.FullPath)
Dim oProcessId = Await DoOpenFile(pDocument.FullPath)
Return New Tuple(Of Integer, String)(oProcessId, pDocument.FullPath)
Catch ex As Exception
Logger.Error(ex)
@@ -131,12 +133,28 @@ Namespace DocumentResultList
End Try
End Function
Private Function DoOpenFile(pFilePath As String) As Process
Dim oProcess = Process.Start(New ProcessStartInfo With {
.FileName = pFilePath
})
Private Async Function DoOpenFile(pFilePath As String) As Task(Of Integer)
Dim _Process = New Process
_Process.StartInfo.FileName = pFilePath
_Process.EnableRaisingEvents = True
Return oProcess
AddHandler _Process.Exited, AddressOf Process_Exited
_Process.Start()
Return _Process.Id
End Function
Private Function Process_Exited(sender As Object, e As EventArgs) As Boolean
Debug.WriteLine("Process is exited")
Dim oProcess As Process = sender
Dim oOpenFile = OpenFiles.
Where(Function(file) file.ProcessId = oProcess.Id).
SingleOrDefault()
oOpenFile.Exited = True
Return True
End Function
Private Sub FileOpenTimer_Elapsed() Handles FileOpenTimer.Elapsed
@@ -154,9 +172,11 @@ Namespace DocumentResultList
' Check if the file is currently in use, and skip if it is.
Dim oIsLocked = FileEx.TestFileIsLocked(oOpenFile.FilePath)
Dim oIsExited = oOpenFile.Exited
Debug.WriteLine($"File is locked: [{oIsLocked}]")
Debug.WriteLine($"File is exited: [{oIsExited}]")
If oIsLocked Then
If oIsLocked Or oIsExited = False Then
Continue For
End If