|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Shared textfile...threadingI 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 Synclock:
Private m_LockingObject as New String = "My Lock" Public Sub WriteLog SyncLock ( m_LockingObject ) .... do stuff End SyncLock End Sub If there are other methods using the log, then wrap those using the same locking object as well. Show quoteHide quote "MobileBoy36" <MobileBo***@gmail.com> wrote in message news:4578437f$0$31464$ba620e4c@news.skynet.be... > 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 > "Robinson" <robinsnewsgro***@hotmail.remove.this.co.uk> schrieb: I'd use 'Private m_Lock As New Object()'.> Private m_LockingObject as New String = "My Lock" -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> Before you go writing something complex, is there a good reason not to
use something like log4net (http://logging.apache.org/log4net/) instead? It much easier than ms's logging solution, and is safe in every way you could want. //Andrew MobileBoy36 wrote: Show quoteHide quote > 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 Hi,
Thanks for the answers. yes I can use a framework for it. But for this case I prefer to use my own class. I want to know what I am doing wrong. I know the the existense of synclock. Using synclock the way you described it seems not to work ( error on lin: fsOut = New System.IO.FileStream(_LogFile,System.IO.FileMode.Append) best regards Show quoteHide quote "Andrew Backer" <awbac***@gmail.com> schreef in bericht news:1165510175.593945.89120@16g2000cwy.googlegroups.com... > Before you go writing something complex, is there a good reason not to > use something like log4net (http://logging.apache.org/log4net/) > instead? It much easier than ms's logging solution, and is safe in > every way you could want. > > //Andrew > > MobileBoy36 wrote: >> 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 > No example of a tested and thread safe sub textfilewriter??
best regards, Mobile boy Show quoteHide quote "MobileBoy36" <MobileBo***@gmail.com> schreef in bericht news:45784fbb$0$1133$ba620e4c@news.skynet.be... > Hi, > > Thanks for the answers. > yes I can use a framework for it. But for this case I prefer to use my own > class. I want to know what I am doing wrong. > > I know the the existense of synclock. > Using synclock the way you described it seems not to work ( error on lin: > fsOut = New System.IO.FileStream(_LogFile,System.IO.FileMode.Append) > > best regards > > > "Andrew Backer" <awbac***@gmail.com> schreef in bericht > news:1165510175.593945.89120@16g2000cwy.googlegroups.com... >> Before you go writing something complex, is there a good reason not to >> use something like log4net (http://logging.apache.org/log4net/) >> instead? It much easier than ms's logging solution, and is safe in >> every way you could want. >> >> //Andrew >> >> MobileBoy36 wrote: >>> 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 >> > > Well, if it's really got yer goat I think you could just look at the
source code for log4net? I always thought that the mutexs = synclock essentially, but here is some extra locking documentation : http://everything2.com/index.pl?node_id=1833821&lastnode_id=0 And please post at least some error message. I am not sure you are actually trying to get help when you just say "it breaks". Please post some actual error information if you want help debugging. //Andrew MobileBoy36 wrote: Show quoteHide quote > No example of a tested and thread safe sub textfilewriter?? > > best regards, > Mobile boy > "MobileBoy36" <MobileBo***@gmail.com> schreef in bericht > news:45784fbb$0$1133$ba620e4c@news.skynet.be... > > Hi, > > > > Thanks for the answers. > > yes I can use a framework for it. But for this case I prefer to use my own > > class. I want to know what I am doing wrong. > > > > I know the the existense of synclock. > > Using synclock the way you described it seems not to work ( error on lin: > > fsOut = New System.IO.FileStream(_LogFile,System.IO.FileMode.Append) > > > > best regards > > > > > > "Andrew Backer" <awbac***@gmail.com> schreef in bericht > > news:1165510175.593945.89120@16g2000cwy.googlegroups.com... > >> Before you go writing something complex, is there a good reason not to > >> use something like log4net (http://logging.apache.org/log4net/) > >> instead? It much easier than ms's logging solution, and is safe in > >> every way you could want. > >> > >> //Andrew > >> > >> MobileBoy36 wrote: > >>> 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 > >> > > > >
Populating combox from dataset
Is this a bug I see before me, or an incomplete understanding of scope? Look at this debugging output about TreeViews How to prevent keydown events on toolbar Apostrophe Problem - HELP Excel dll on computer whitout offices Visual Basic 2005 - ContextSwitchDeadLock was detected when debugging Richtextbox SaveFile Release VB.NET Problem: Setting the Mask and Picture Properties on a CommadBar without "LoadPicture"??? In VB.NET what do I need to "import" to get stdole ? |
|||||||||||||||||||||||