'This variable helps eliminate the duplicate/multiple firing of the event
Dim oRunningFiles As New Dictionary(Of String, Threading.Thread)
Dim oFileSystemADT As FileSystemWatcher
Sub StartIt()
oRunningFiles.Clear()
dLastError = "1900-01-01 00:00:00"
oFileSystemADT = New FileSystemWatcher("\\filepath\OneC_transagt_ADT$")
AddHandler oFileSystemADT.Created, AddressOf OnCreated
AddHandler oFileSystemADT.Changed, AddressOf OnChanged
AddHandler oFileSystemADT.Error, AddressOf OnError
oFileSystemADT.IncludeSubdirectories = True
oFileSystemADT.EnableRaisingEvents = True
Dim myFile = Directory.GetFiles("\\filepath\OneC_transagt_ADT$").OrderByDescending(Function(f) New FileInfo(f).LastWriteTime).First()
If Len(myFile) > 0 Then
'FunWatchFile(myFile)
OnChanged(Nothing, New FileSystemEventArgs(WatcherChangeTypes.Changed, My.Computer.FileSystem.GetParentPath(myFile), Path.GetFileName(myFile)))
Else
oFileSystemADT.WaitForChanged(WatcherChangeTypes.Created Or
WatcherChangeTypes.Changed)
End If
End Sub
Public Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
'Console.WriteLine("File created: " & e.FullPath & " change type: " & e.ChangeType)
'Dim oThread As Threading.Thread
'oThread = New Threading.Thread(Sub() FunWatchFile(e.FullPath))
'oThread.Name = e.FullPath
'oThread.Start()
'oThreads.Add(oThread)
Console.WriteLine("File created: " & e.FullPath & " change type: " & e.ChangeType)
OnChanged(source, e)
'Dim oThread As Threading.Thread
'oThread = New Threading.Thread(Sub() FunWatchFile(e.FullPath))
'oThread.Name = e.FullPath
'oThread.Start()
'oThreads.Add(oThread)
End Sub
Private Sub OnError(source As Object, e As ErrorEventArgs)
'If e.GetException.GetType() = TypeOf (InternalBufferOverflowException) Then
' txtResults.Text += "Error: File System Watcher internal buffer overflow at " + DateTime.Now + "\r\n"
'Else
' txtResults.Text += "Error: Watched directory not accessible at " + DateTime.Now + "\r\n"
'End If
iErrorCount += 1
RemoveHandler oFileSystemADT.Created, AddressOf OnCreated
RemoveHandler oFileSystemADT.Changed, AddressOf OnChanged
RemoveHandler oFileSystemADT.Error, AddressOf OnError
funSendMail("MonitorADT@crouse.org", "richlemmermann@crouse.org", "MPF ADT OnError (live mode)", "Going to restart the Handler momentarily. Error count = " & iErrorCount & " minutes", False)
oFileSystemADT.IncludeSubdirectories = False
oFileSystemADT.EnableRaisingEvents = False
oFileSystemADT.Dispose()
Threading.Thread.Sleep(2000)
StartIt()
End Sub
' DateTime lastRead = DateTime.MinValue;
'void OnChanged(Object source, FileSystemEventArgs a)
'{
' DateTime lastWriteTime = File.GetLastWriteTime(Uri);
' If (lastWriteTime!= lastRead)
' {
' doStuff();
' lastRead = lastWriteTime;
' }
' // else discard the (duplicated) OnChanged event
'}
Dim lastRead = DateTime.MinValue
Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
' oFileSystemADT.EnableRaisingEvents = False
Console.WriteLine("File changed: " & e.FullPath & " change type: " & e.ChangeType)
'Are we already processing it?
' DateTime lastWriteTime = File.GetLastWriteTime(Uri);
Dim lastWriteTime As DateTime = File.GetLastWriteTime(e.FullPath)
'If Not bAlreadyRUnning(e.FullPath) And Not bInProgress Then
bInProgress = True
'OnCreated(source, e)
Dim oThread As Threading.Thread
oThread = New Threading.Thread(Sub() FunWatchFile(e.FullPath))
oThread.Name = e.FullPath
Try
Console.WriteLine("e.Name: " & e.Name)
oRunningFiles.Add(e.Name, oThread)
oThread.Start()
Catch ex As Exception
Console.WriteLine("** Dup? " & e.FullPath)
End Try
' oFileSystemADT.EnableRaisingEvents = True
'oThreads.Add(oThread)
bInProgress = False
lastRead = lastWriteTime
'bInProgress
'Threading.Thread.Sleep(1000)
' End If
End Sub
Function FunWatchFile(sFile As String)
Console.WriteLine("Watching: " & sFile)
'While IsFileOpen(New FileInfo(sFile))
' Threading.Thread.Sleep(2000)
'End While
'If bAlreadyRUnning(sFile) Then
' Console.WriteLine("Duplicate, returning")
' Return ""
'End If
Dim sLastFile As String = ""
Try
Using reader As StreamReader = New StreamReader(New FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Console.WriteLine("sFile: " & sFile)
Dim lastMaxOffset As Long = reader.BaseStream.Length
lastMaxOffset = reader.BaseStream.Length
While True And Not bSelfDestruct
System.Threading.Thread.Sleep(100)
If (reader.BaseStream.Length = lastMaxOffset) Then
Else
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin)
' //read out of the file until the EOF
Dim line As String = ""
Do
line = reader.ReadLine()
If InStr(line, "no mapping d", CompareMethod.Text) > 0 Then
'funSendMail("MonitorADT@crouse.org", "richlemmermann@crouse.org", "MPF ADT Error (debug mode)", "", False)
Console.WriteLine("RTL> " & line)
If dLastError = "1900-01-01 00:00:00" Then dLastError = Now
If DateDiff(DateInterval.Minute, dLastNotified, Now) > iNotifiyInMins Then
funSendMail("MonitorADT@crouse.org", "dlrevcycle@crouse.org;dlitinterfaces@crouse.org;dlsysadmin@crouse.org;dlithelpdesk@crouse.org", "MPF ADT Error (live mode)", "Hi all" & vbCrLf & " This message is saying that there is a problem with the ADT feed and OneContent. Server is: 'CRH19VMPFagent1' and Service is: 'OneC_transagt_ADT'" & vbCrLf & vbCrLf & " Please restart that service so that the error clears." & vbCrLf & "** If service hangs, run from Admin Command Prompt = 'taskkill /F /IM transagt.exe'" & vbCrLf & vbCrLf & "Interfaces will have to replay the ADT" & vbCrLf & vbCrLf & "Mel's team can provide the pertinent information" & vbCrLf & vbCrLf & "Line that started this is: " & vbCrLf & vbCrLf & line & vbCrLf & vbCrLf & "FileName: " & sFile & vbCrLf & vbCrLf & "First Error Time: " & dLastError & vbCrLf & vbCrLf, False)
dLastNotified = Now
Else
End If
ElseIf InStr(line, "0:INFO Transaction Agent Stopped", CompareMethod.Text) > 0 Then
dLastError = "1900-01-01 00:00:00"
End If
If InStr(line, "0:TRANEND", CompareMethod.Text) > 0 Then
Console.WriteLine("RTL> " & Path.GetFileName(sFile) & "," & line)
End If
'Loop While Len(line) > 0 And Not bSelfDestruct
oListOfLinesForWebPage.Add(line)
Loop While Not reader.EndOfStream And Not bSelfDestruct
lastMaxOffset = reader.BaseStream.Position
iListOffset = lastMaxOffset
' Console.WriteLine(line);
'If InStr(line, "0:TRANEND", CompareMethod.Text) > 0 Then
' Console.WriteLine("RTL#2> " & Path.GetFileName(sFile) & "," & line)
'End If
End If
End While
End Using
Catch ex As Exception
Console.WriteLine("** Error (" & sFile & "): " & ex.Message.ToString)
End Try
' Using (StreamReader reader = New StreamReader(New FileStream(fileName,
' FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) )
'{
' //start at the end of the file
' Long lastMaxOffset = reader.BaseStream.Length;
' While (True)
' {
' System.Threading.Thread.Sleep(100);
' //if the file size has Not changed, idle
' If (reader.BaseStream.Length == lastMaxOffset)
' Continue Do;
' //seek to the last max offset
' reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
' //read out of the file until the EOF
' String line = "";
' While ((line = reader.ReadLine())!= null)
' Console.WriteLine(line);
' //update the last max offset
' lastMaxOffset = reader.BaseStream.Position;
' }
'}
Console.WriteLine("**Thread Aborting: " & Threading.Thread.CurrentThread.ManagedThreadId)
'This line causes a crash. Cannot reset the name
'Threading.Thread.CurrentThread.Name = "Aborted: " & Now
Threading.Thread.CurrentThread.Abort()
End Function