472,995 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How do you synchronize closing a synchronized TextWriter?

If I create a synchronized TextWriter with TextWriter.Synchronized(...), how
do I make sure that all the writes are done before I Close()? Do I have to
invent that synchronization mechanism myself? If I do that, then I'm going
to have to be locking some sort of "streamIsOpen" resource and if I do that,
then I don't need a synchronized TextWriter which makes me wonder, what good
is TextWriter.Synchronized()?
Jul 16 '08 #1
1 3821
On Wed, 16 Jul 2008 15:08:23 -0700, John Vottero <JV******@mvpsi.com>
wrote:
If I create a synchronized TextWriter with TextWriter.Synchronized(...),
how do I make sure that all the writes are done before I Close()? Do I
have to invent that synchronization mechanism myself? If I do that,
then I'm going to have to be locking some sort of "streamIsOpen"
resource and if I do that, then I don't need a synchronized TextWriter
which makes me wonder, what good is TextWriter.Synchronized()?
All it does is allow you to write to the writer from multiple threads
without synchronizing the writes yourself. Even if you'd implemented that
yourself, that wouldn't address the question of one thread closing the
object before another's attempt to write has completed.

As for how to actually address the issue, I suppose there are a variety of
ways. But one approach would be to simply track how many threads have the
writer object for their use and provide a signaling mechanism to the
threads to alert them to the need to close the writer. Then when all the
threads have acknowledged the signal (perhaps by decrementing a counter),
you can safely close the writer.

Alternatively, you could just close the writer and not worry about
outstanding writes. The instance methods themselves will be thread-safe,
so at worst some write will throw an exception (probably
ObjectDisposedException) rather than complete successfully. Barring a
sync/signal mechanism such as I described above, that would always be a
risk anyway. So the default behavior should work fine for you in that
case.

Pete
Jul 16 '08 #2

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

Similar topics

8
by: Thomas Rademacher | last post by:
Hallo, I want to synchronize the main function of my script. I havn't a class structure in my script. def main(): # my code to synchronized if __name__=='__main__': main()
4
by: gene.ellis | last post by:
Using a web interface, I am placing text into a SQL database. From time to time, I would like to synchronize one of my other tables in the database with the table that I am inserting content into....
1
by: scott.ballard | last post by:
Greetings, I am using the .Net Framework XmlSerializer to serialize a class that contains some string properties. The problem is that when a string contains a zero-length string ("") it is...
8
by: Zlatko Matiæ | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
6
by: Fabian Härle | last post by:
Hi, if I write to a tet file via a System.IO.TextWriter the changes to the file are not done before I close the file. Is there a possibility to always save the file with every change? The point...
1
by: andpapas | last post by:
In my multi-threaded application i'm getting to a point where I've got the exception bellow: ------------------------------------------------------------------------...
1
by: taleran58 | last post by:
Hi all. I'm writting a multithreading program that will update a loacal Access database. I'm using OleDB connection to get access to the database from each thread and my question is: how do I...
2
by: Mark | last post by:
I started to use MS Visual Basic 2005 Express Edition to learn how to program in Visual Basic. This Express Edition saves projects in this path: C:\Documents and Settings\Username\My...
1
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 ?...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.