I really thank you for your help.
I have set the script as you detailled but I get this error when building :
Severity Code Description Project File Line Suppression State
Warning CA2202 Object 'objFileStream' can be disposed more than once in method 'P3DBridge.watcher_Changed(Object, FileSystemEventArgs)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 162
Line 162 is the second "End using" of the Private shared Sub watcher_Changed
Also, I tried to add the with event... but not managed to script it properly.
I think that I'm not far from having all this working with all the help you provided me.
Here is what my code actually looks like :
Public Class P3DBridge
Private Const watchedFile As String = "...\P3DBridge.txt"
'This will store the latest values from the file
Private Shared currentFileValues As New Dictionary(Of String, Decimal)
'This is the watcher object, declared 'WithEvents'... see code at the bottom
Private Shared WithEvents watcher As New IO.FileSystemWatcher
Public Shared Function VA_DisplayName() As String
'...
End Function
Public Shared Function VA_Id() As Guid
'...
End Function
Public Shared Function VA_DisplayInfo() As String
'...
End Sub
Public Shared Sub VA_Init1(vaProxy As Object)
Dim objStreamReader As System.IO.StreamReader
objStreamReader = New System.IO.StreamReader(".../P3DBridgeInit.txt")
Dim P3DSeason As String
P3DSeason = objStreamReader.ReadLine
Convert.ToDecimal(P3DSeason)
vaProxy.SetDecimal("VASeason", P3DSeason)
Dim P3DAirType As String
P3DAirType = objStreamReader.ReadLine
vaProxy.SetText("VAAirType", P3DAirType)
Dim P3DAirModel As String
P3DAirModel = objStreamReader.ReadLine
vaProxy.SetText("VAAirModel", P3DAirModel)
objStreamReader.Close()
'Sets the directory and files to watch; since Filter is set with a specific file name,
'and not a wildcard mask, only a single file will be monitored
watcher.Path = IO.Path.GetDirectoryName(watchedFile)
watcher.Filter = IO.Path.GetFileName(watchedFile)
watcher.EnableRaisingEvents = True
End Sub
Public Shared Sub VA_Exit1(vaProxy As Object)
'...
End Sub
Public Shared Sub VA_Invoke1(vaProxy As Object)
'The currentFileValues dictionary contains all the latest values, you can simply return them
For Each kvp As KeyValuePair(Of String, Decimal) In currentFileValues
vaProxy.SetDecimal(kvp.Key, kvp.Value)
Next
End Sub
Private Shared Sub watcher_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles watcher.Changed
'This is the event fired every time file changes are committed to disk
Using objFileStream = New IO.FileStream(watchedFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
Using objStreamReader = New IO.StreamReader(objFileStream)
Dim P3Dvalues As String()
P3Dvalues = objStreamReader.ReadLine().Split("|"c)
currentFileValues.Clear()
currentFileValues.Add("VADaytime", Convert.ToDecimal(P3Dvalues(0)))
currentFileValues.Add("VAstatus", Convert.ToDecimal(P3Dvalues(1)))
currentFileValues.Add("VAtastat", Convert.ToDecimal(P3Dvalues(2)))
currentFileValues.Add("VAcrstat", Convert.ToDecimal(P3Dvalues(3)))
End Using
End Using
End Sub
End Class