Flushing a stream

kejpa

Well-known member
Joined
Oct 10, 2003
Messages
320
Hi,
I want to make sure that my trace.Writelines are properly written to my trace file and so I created a Threading.Timer like...
Code:
_trdStreamFlusher = New Threading.Timer(AddressOf StreamFlusher, _Stream, 10000, 10000)

 -Snip-
    Private Shared Sub StreamFlusher(ByVal file As Object)
        Dim oStream As IO.FileStream = DirectCast(file, IO.FileStream)
        Try
            oStream.Flush()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
but the file dont contain all my trace information :(

Any ideas?
TIA
/Kejpa
 
Could you not either just call flush when you add data to the stream or alternatively one of the overloads for the FileStreams constructor allows you to specify the buffer size so you could choose a more appropriate value.

Also IIRC instance members of the FileStream object are not thread safe - therefore you could hit a race condition if the timer fires at the same time as you are adding data to the stream.
 
Back
Top