VB.Net - Thread Synclock usage

kepler

New member
I want to do this..
Code:
    Public Shared Sub log(ByVal p_strMessage As String)
          Dim oWrite As System.IO.StreamWriter = System.IO.File.AppendText(Location)
          Dim strLogEntry As String
          strLogEntry = DateTime.Now.ToString & ":  " & p_strMessage & Environment.NewLine

          oWrite.Write(strLogEntry)
          oWrite.Flush()
          oWrite.Dispose()
          oWrite = Nothing
    End Sub
but my app spawns multiple threads that want to access the log and so can cause multi-access problems. So, I thought Synclock would be the way to go. However, as the sub is shared I can't synclock it where it is being called, yet can't Synclock oWrite within the sub as it would need to be instatiated before the synclock block, which would be enough to cause concurrent access errors.

I thought something like
Code:
    Public Shared Sub log(ByVal p_strMessage As String)
        Dim token As New Object

        SyncLock token
            Dim oWrite As System.IO.StreamWriter = System.IO.File.AppendText(Location)
            Dim strLogEntry As String
            strLogEntry = DateTime.Now.ToString & ":  " & p_strMessage & Environment.NewLine

            oWrite.Write(strLogEntry)
            oWrite.Flush()
            oWrite.Dispose()
            oWrite = Nothing
        End SyncLock

    End Sub

would help, but no luck - still have concurrent access errors raised on the instatiation of oWrite.

Any ideas?
 
I've also tried
Code:
    Public Shared Sub log(ByVal p_strMessage As String)
        Dim strLogEntry As String
        Dim oWrite As New System.IO.StreamWriter(Location)       

        oWrite = System.IO.StreamWriter.Synchronized(System.IO.File.AppendText(Location))
        strLogEntry = DateTime.Now.ToString & ":  " & p_strMessage & Environment.NewLine

        oWrite.Write(strLogEntry)
        oWrite.Flush()
        oWrite.Dispose()
        oWrite = Nothing

    End Sub
but when the logging object initialises it writes a message to the log file but in this form it raises an IOException against

oWrite = System.IO.StreamWriter.Synchronized(System.IO.File.AppendText(Location))

saying the file is in use by another process, which it bloody well isn't. >_<
 
Think I've figured it out

Code:
Public Class cLogger

    Private Shared writer As TextWriter

    ...

    Public Shared Sub Initialise()
        writer = System.IO.StreamWriter.Synchronized(System.IO.File.AppendText(Location))
        If logFileExists() Then
            log("Test began")
        Else
            createNewLogFile()
        End If
    End Sub

    ...

    Public Shared Sub log(ByVal p_strMessage As String)
        Dim strLogEntry As String = DateTime.Now.ToString & ":  " & p_strMessage & Environment.NewLine

        writer.Write(strLogEntry)
        writer.Flush()
    End Sub

    ...
End Class ' cLogger
*fingers crossed*
 
What ya should do is have a buffer created in memory contained by a thread-safe mechanism. Then, you create another thread which every so often, or whenever nothing is happening, dumps the buffer onto a file. This way you can have several transactions happen while not thrashing the HD. The thread-safe mechanism should be made such that only one thread may have complete access to the data until that thread is done with the data.

Thread 1
loop
If DataNotLocked
Lock Data
Write Data
Unlock Data
end if
repeat loop
----------
Thread 2
loop
If DataNotLocked
Lock Data
Write Data
Unlock Data
end if
repeat loop



Locking Data must be atomic


Hope it helps. Didnt really read what you are doing cause it's VB. this is my 2c. ;)
 
Back
Top