473,651 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronized StreamWriter - Is Flush ThreadSafe?

Hi all,

I'm accessing a synchronized Streamwriter from potentially several threads
to do some logging. Is the Flush command threadsafe too? Should I single
thread the Flush command or will the Synchronized streamwriter handle that
for me?

Thanks!
Jun 6 '07 #1
5 5723

How are you writing to the log? I have public methods that are synclocked
on the log instance for writing in my class. You can also make the stream
auto-flush by default which is handy.
Jun 6 '07 #2
"Robin Tucker" <rt******@hotma il.comwrote in
news:f4******** ***********@new s.demon.co.uk:
>
How are you writing to the log? I have public methods that are
synclocked on the log instance for writing in my class. You can also
make the stream auto-flush by default which is handy.
The Synchronized Stream doesn't seem to support Auto-Flush.

I'm not synclocking the StreamReader because I heard it is still "unsafe".
Rather I'm using the thread-safe synchronized wrapper.

So I like to know if the threadsafe wrapper wraps everything ... or just
..write.
Jun 6 '07 #3

No I mean you have a class that wraps the stream and synclock that, so
writers are never accessing the stream directly. It is privately
encapsulated. For example, instead of writing Debug.Writeline (), the coder
will use MyTracer.WriteL ine (), where MyTracer is your accessor class that
does the synclock.
Jun 6 '07 #4
"Robin Tucker" <rt******@hotma il.comwrote in
news:f4******** ***********@new s.demon.co.uk:
>
No I mean you have a class that wraps the stream and synclock that, so
writers are never accessing the stream directly. It is privately
encapsulated. For example, instead of writing Debug.Writeline (), the
coder will use MyTracer.WriteL ine (), where MyTracer is your accessor
class that does the synclock.
I could do that - but the Synchronized Stream is SUPPOSED to be threadsafe.
So I'm just trying to confirm the fact about the .flush.

There's no sense in rewrapping a threadsafe class right?
Jun 6 '07 #5
"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Robin Tucker" <rt******@hotma il.comwrote in
news:f4******** ***********@new s.demon.co.uk:
>>
No I mean you have a class that wraps the stream and synclock that, so
writers are never accessing the stream directly. It is privately
encapsulated . For example, instead of writing Debug.Writeline (), the
coder will use MyTracer.WriteL ine (), where MyTracer is your accessor
class that does the synclock.

I could do that - but the Synchronized Stream is SUPPOSED to be
threadsafe.
So I'm just trying to confirm the fact about the .flush.

There's no sense in rewrapping a threadsafe class right?
Well that depends on what else you are doing with the stream. If it's nout,
then you should be fine with it.
Jun 6 '07 #6

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

Similar topics

1
3553
by: Vladimir Bezugliy | last post by:
I have a server that listen a socket. And I have next client: TcpClient socketForServer = new TcpClient("IVBEZUGLIY", 21000); NetworkStream networkStream = socketForServer.GetStream(); StreamWriter streamWriter = new StreamWriter(networkStream); streamWriter.Write("Hello12345!!!"); streamWriter.Flush(); //streamWriter.Close();
1
717
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a streamwriter writing to a network share, is it possible for the network to go down in such a way that the tartet file is only partialy written? or are there some kind of check sums to prevent this.
3
5015
by: José Joye | last post by:
I have 2 threads using the same StreamWriter (obtained from FileInfo.AppendText). This first thread will always use the WriteLine() method and the second thread will always use the Flush() method. At the present, I have protected the access to the StreamWriter using a Lock() section. Is this really particular case, is this necessary? What could be the implication if I remove the Lock() section?
4
7702
by: ASP Yaboh | last post by:
I am using StreamWriter's .Writeline() method to create a simple text file. I found that ASCII characters 255 & 254 are being inserted as the first two characters of the file which causes a 3rd-party users' parser to fail. It is not being inserted by my data as the first line starts with "LOGIN: ", i.e. hard-coded. Has anyone else encountered this? Is there any filtering settings I can apply? I looked through the documentation on
1
3430
by: andpapas | last post by:
In my multi-threaded application i'm getting to a point where I've got the exception bellow: ------------------------------------------------------------------------ System.ObjectDisposedException: Cannot write to a closed TextWriter. Server stack trace: at System.IO.__Error.WriterClosed() at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(Char buffer, Int32 index, Int32
1
3983
by: Max Powers | last post by:
Hello, I have a VB.NET code that creates a StreamWriter file first and then it does a series of procedures that write lines for this StreamWriter. When all the procedures are done, the StreamWriter is flushed and closed and all the lines get written to the text. However, if for some reason one of the procedures fails. The StreamWriter doesn't write anything at all and the file ends up empty. Is there a way to save (or actually write)...
4
8420
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
7
2952
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I'm building an ASP.NET site which is hosted on a remote server. When I try to create a new file using StreamWriter I must specify the drive location where the file will be saved. I don't have file access anywhere on the remote server, and therefore I can't figure out how I can create a file. Is it possible to utilize StreamWriter to create a new file and somehow display it for the user to save, without actually saving the file...
1
4393
by: fniles | last post by:
If I use TextWriter synchronized method like below codes if another thread is trying to write at the same time, what will happen ? Will it the 2nd thread request to write to the file be queued ? Will the 2nd thread then stop processing other codes while waiting to write to this file ? Thank you. Dim swLogOrig As StreamWriter Dim swLog As TextWriter
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.