M
MobileBoy36
Hi All,
I want to make a LogFile class that is thread safe. I use a Mutex for it.
But the behavior of the class is not that normal.
In a c# guide I read you can achieve it by simply using Mutex.Waitone and
Mutex.ReleaseMutex. Is that right?
What 's wrong with my sub Writelog then?
Best regards,
Mobileboy
Public Class LogFile
Private _LogFile As String
Private _LoggingLevel As Integer = 99
Private Shared _Mutex As System.Threading.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threading.Mutex
End If
End Sub
Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFromThisLogging As Integer = 99)
Dim fsOut As System.IO.FileStream
Dim MyStreamWriter As System.IO.StreamWriter
Dim Datum As DateTime
Dim strDatum As String
_Mutex.WaitOne() ' Waitone wacht tot wanneer hij de mutex kan
verwerven, zolang een ander proces hem niet heeft vrij gegeven
If (_LoggingLevel >= LoggingLevelFromThisLogging) Or
(LoggingLevelFromThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File.Exists(_LogFile) Then
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Append)
Else
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Create)
End If
MyStreamWriter = New System.IO.StreamWriter(fsOut)
strDatum = Lzero(CStr(Datum.Day), 2) & "-" & _
Lzero(CStr(Datum.Month), 2) & "-" & _
CStr(Datum.Year) & " " & _
Lzero(CStr(Datum.Hour), 2) & ":" & _
Lzero(CStr(Datum.Minute), 2) & ":" & _
Lzero(CStr(Datum.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text)
MyStreamWriter.WriteLine(Text)
MyStreamWriter.Flush()
MyStreamWriter.Close()
fsOut.Close()
End If
_Mutex.ReleaseMutex() ' De mutex weer vrijgegven, maw aangeven
dat er geen andere thread meer mee bezig is
End Sub
I want to make a LogFile class that is thread safe. I use a Mutex for it.
But the behavior of the class is not that normal.
In a c# guide I read you can achieve it by simply using Mutex.Waitone and
Mutex.ReleaseMutex. Is that right?
What 's wrong with my sub Writelog then?
Best regards,
Mobileboy
Public Class LogFile
Private _LogFile As String
Private _LoggingLevel As Integer = 99
Private Shared _Mutex As System.Threading.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threading.Mutex
End If
End Sub
Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFromThisLogging As Integer = 99)
Dim fsOut As System.IO.FileStream
Dim MyStreamWriter As System.IO.StreamWriter
Dim Datum As DateTime
Dim strDatum As String
_Mutex.WaitOne() ' Waitone wacht tot wanneer hij de mutex kan
verwerven, zolang een ander proces hem niet heeft vrij gegeven
If (_LoggingLevel >= LoggingLevelFromThisLogging) Or
(LoggingLevelFromThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File.Exists(_LogFile) Then
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Append)
Else
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Create)
End If
MyStreamWriter = New System.IO.StreamWriter(fsOut)
strDatum = Lzero(CStr(Datum.Day), 2) & "-" & _
Lzero(CStr(Datum.Month), 2) & "-" & _
CStr(Datum.Year) & " " & _
Lzero(CStr(Datum.Hour), 2) & ":" & _
Lzero(CStr(Datum.Minute), 2) & ":" & _
Lzero(CStr(Datum.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text)
MyStreamWriter.WriteLine(Text)
MyStreamWriter.Flush()
MyStreamWriter.Close()
fsOut.Close()
End If
_Mutex.ReleaseMutex() ' De mutex weer vrijgegven, maw aangeven
dat er geen andere thread meer mee bezig is
End Sub