Say I have code that does this:
Public Function AppendToLogFile(ByVal s As String) As Boolean Dim success As Boolean = True Dim fs As IO.FileStream = Nothing Dim sw As IO.StreamWriter = Nothing Static LogFileLock As New Object() SyncLock LogFileLock Try fs = New IO.FileStream(LogFilePath) sw = New IO.StreamWriter(fs) sw.WriteLine(s) Catch ex As Exception success = False Finally If Not sw Is Nothing Then sw.Close() If Not fs Is Nothing Then fs.Close() End Try End SyncLock Return success End Function
First of all: the problem is that I have what Try / Catch / finally blocks inside SyncLock?
Secondly: suppose this code is triggered in an event, potentially many times over a short timeframe, say ten times in one second. Is it good SyncLock, or does it make sense to add a line to the queue, and then write all the lines from the queue to a timer file that turns off, say, every second?
source share