|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Thread safe TextWriter questionIs this the correct syntax to make a Textwriter thread safe ? Thank you.
Dim swError As TextWriter Dim swErrorSync As TextWriter swError = New StreamWriter(Application.StartupPath & "\Log.txt", True) swErrorSync = swError.Synchronized(swError) swError.Write(Now & vbCrLf) swError.Close() "fniles" <fni***@pfmail.com> wrote in message No. Not quite. Threads should write to swErrorSync. And it's not clear news:OuPMeONtGHA.4748@TK2MSFTNGP03.phx.gbl... > Is this the correct syntax to make a Textwriter thread safe ? Thank you. > > Dim swError As TextWriter > Dim swErrorSync As TextWriter > swError = New StreamWriter(Application.StartupPath & "\Log.txt", True) > swErrorSync = swError.Synchronized(swError) > swError.Write(Now & vbCrLf) > swError.Close() > what code different threads are running. This snippet, as a whole, is definitely not thread-safe. David I am sorry, I typed in incorrectly.
Pls tell me if the following is correct and thread safe. Thanks. Dim swError As TextWriter Dim swErrorSync As TextWriter swError = New StreamWriter(Application.StartupPath & "\Log.txt", True) swErrorSync = swError.Synchronized(swError) swErrorSync .Write(Now & vbCrLf) swError.Close() swErrorSync.Close() Show quoteHide quote "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in message news:u%232MdXNtGHA.2020@TK2MSFTNGP03.phx.gbl... > > "fniles" <fni***@pfmail.com> wrote in message > news:OuPMeONtGHA.4748@TK2MSFTNGP03.phx.gbl... >> Is this the correct syntax to make a Textwriter thread safe ? Thank you. >> >> Dim swError As TextWriter >> Dim swErrorSync As TextWriter >> swError = New StreamWriter(Application.StartupPath & "\Log.txt", True) >> swErrorSync = swError.Synchronized(swError) >> swError.Write(Now & vbCrLf) >> swError.Close() >> > > No. Not quite. Threads should write to swErrorSync. And it's not clear > what code different threads are running. This snippet, as a whole, is > definitely not thread-safe. > > David >
Show quote
Hide quote
"fniles" <fni***@pfmail.com> wrote in message No. That code is not thread-safe. If multiple threads execute that code news:uyKbF5NtGHA.644@TK2MSFTNGP03.phx.gbl... >I am sorry, I typed in incorrectly. > Pls tell me if the following is correct and thread safe. > Thanks. > > Dim swError As TextWriter > Dim swErrorSync As TextWriter > > swError = New StreamWriter(Application.StartupPath & "\Log.txt", True) > swErrorSync = swError.Synchronized(swError) > swErrorSync .Write(Now & vbCrLf) > swError.Close() > swErrorSync.Close() you will not necessarilly get correct, whole, ordered log entries. If you refactored the code so that a single thread opened and closed the file, while multiple threads accessed the syncronized StreamWriter wrapper, then it would be. David
Transparent backcolor
Threads difference defaultdataview and a normal dataview? Problem with System.IO.File.Move and Catching Exceptions onRollBack event Sending chr(255) to serial port Creating ToolWindows with .NET in VBE Select node Using Systems.Management vs. WbemScripting How do you set file permissions? |
|||||||||||||||||||||||