473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Thread safe TextWriter question

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()
Jul 31 '06 #1
3 1582

"fniles" <fn****@pfmail.comwrote in message
news:Ou**************@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
Jul 31 '06 #2
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()

"David Browne" <davidbaxterbrowne no potted me**@hotmail.comwrote in
message news:u%****************@TK2MSFTNGP03.phx.gbl...
>
"fniles" <fn****@pfmail.comwrote in message
news:Ou**************@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

Jul 31 '06 #3

"fniles" <fn****@pfmail.comwrote in message
news:uy*************@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()


No. That code is not thread-safe. If multiple threads execute that code
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
Jul 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
3
by: tcomer | last post by:
Hello! I'm working on an asynchronous network application that uses multiple threads to do it's work. I have a ChatClient class that handles the basic functionality of connecting to a server and...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
2
by: kengtung | last post by:
Greetings all, I just started programming with C# (.Net FW 1.1) last year and still learning. Previously I am using VB 6.0. Recently, in one application, I encountered a C# error "Cannot write...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
5
by: Neil | last post by:
Hi, Long story short, we use "Server.Execute" to grab the output from an ASPX page ... Server.Execute("page.aspx", textWriter); We've noticed that intermittantly, we get a "Thread was being...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.