473,486 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Streamwriter Close

I am having a very strange problem with StreamWriter and writing to files.
I have a procedure that is called several times to write out seperate files,
each of them uniquely named by date all they way to the one hundred
thousandth of a second. After the data is encrypted and written, the
StreamWriter is closed and the function is returned. This all works fine
and dandy, except when it is called more than once in succession.
Originally, the file append flag in the StreamWriter constructor was
accidentally left to true and I was getting some files where there was two
files worth of data in them. After turning the append flag off, I am
usually short one or two files because it can't append.

I have solved the problem temporarily by putting a Thread.Sleep(100) in
before creating the new StreamWriter object, but with the volume of file
writing that will be occuring when this goes public, it will cause too much
of a slow down. Is there a way to safely write data to a file, close it and
ensure that the next file write isnt going to step on the current one?

Thank you for any help you can provide.

Bob Hansen
Nov 22 '05 #1
2 2161
Bob Hansen <bh********@jascorp.com> wrote:
I am having a very strange problem with StreamWriter and writing to files.
I have a procedure that is called several times to write out seperate files,
each of them uniquely named by date all they way to the one hundred
thousandth of a second. After the data is encrypted and written, the
StreamWriter is closed and the function is returned. This all works fine
and dandy, except when it is called more than once in succession.
Originally, the file append flag in the StreamWriter constructor was
accidentally left to true and I was getting some files where there was two
files worth of data in them. After turning the append flag off, I am
usually short one or two files because it can't append.

I have solved the problem temporarily by putting a Thread.Sleep(100) in
before creating the new StreamWriter object, but with the volume of file
writing that will be occuring when this goes public, it will cause too much
of a slow down. Is there a way to safely write data to a file, close it and
ensure that the next file write isnt going to step on the current one?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Bob Hansen <bh********@jascorp.com> wrote:
I am having a very strange problem with StreamWriter and writing to files.
I have a procedure that is called several times to write out seperate files,
each of them uniquely named by date all they way to the one hundred
thousandth of a second. After the data is encrypted and written, the
StreamWriter is closed and the function is returned. This all works fine
and dandy, except when it is called more than once in succession.
Originally, the file append flag in the StreamWriter constructor was
accidentally left to true and I was getting some files where there was two
files worth of data in them. After turning the append flag off, I am
usually short one or two files because it can't append.

I have solved the problem temporarily by putting a Thread.Sleep(100) in
before creating the new StreamWriter object, but with the volume of file
writing that will be occuring when this goes public, it will cause too much
of a slow down. Is there a way to safely write data to a file, close it and
ensure that the next file write isnt going to step on the current one?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3

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

Similar topics

4
2440
by: Majed | last post by:
Hi , all I'm trying to write unicode to a file for another app (not developed with vs2003) to read it. I used StreamWriter with unicode encoding.but I was surprised that the streamwriter adds FFFE...
1
9417
by: James | last post by:
Hi, I am new to .NET framework. I am trying to get data from the third party web application using WebHTTPRequest and Response class. I am getting TimeOut exception at GetResponse Method.. ...
1
375
by: Bob Hansen | last post by:
I am having a very strange problem with StreamWriter and writing to files. I have a procedure that is called several times to write out seperate files, each of them uniquely named by date all they...
1
1783
by: Daniel | last post by:
i would like to konw when the data sent so that i can close the streamwriter and networkstream is there some sort of call backs/events i have to implement for this to work? if so how? can i just...
3
2332
by: W Akthar | last post by:
Hi I am trying to create a windows service which queries SQL Server on timed intervals and depending on the results send appointments to Outlook. The problem lies when I try to create an...
1
4655
by: R.L. | last post by:
See the code below, var 'content ' is suppose to be "Hello!", not "". Who knows why? Thanks ---------------------------------------- string text = "hello!"; MemoryStream stream = new...
2
1887
by: Bruce Wiebe | last post by:
hi all im having a problem accessing a text file on my hard disk after ive created it and added some text to it it would appear that the file is still locked. What happens is this i have...
4
8407
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();
1
1323
by: asem0525 | last post by:
Hello guys, I have this kind of problem: first, the textBox tbISBN has a value that is read from the text file, but then after pressing the "Clear" button, it should be realized, and when I enter...
2
7141
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...
0
7100
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,...
0
7175
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...
1
6842
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
7330
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...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
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...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
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...

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.