473,549 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ 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(10 0) 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 2172
Bob Hansen <bh********@jas corp.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(10 0) 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Bob Hansen <bh********@jas corp.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(10 0) 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.co m>
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
2447
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 to the start of the file,which stopes the other app from reading it!! any idea how to stope it frome doing that,do I have to use another class...
1
9433
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.. Dim objResponse As HttpWebResponse = objRequest.GetResponse() tried out different solutions but still problem persist. Here is the
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 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. ...
1
1799
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 open neworkstream and streamwriter, send data and then close it syncrhronously or do i have to implement some callbacks/events to do this like in vb6?...
3
2339
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 outlook application object.
1
4664
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 MemoryStream(); StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII); streamWriter.Write(text);
2
1891
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 three buttons on a windows form that provide three options for the user the process goes like this user clicks button one and a web service is called...
4
8415
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
1331
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 the new text into the textBox it should get the new text that I entered, but it keeps it's first value, even after the "Clear" button event. Here is...
2
7148
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 to a closed TextWriter.". I looked at the code and it looks ok to me. Maybe I am missing some understanding here on the inner function of...
0
7524
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...
0
7451
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...
1
7475
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...
0
7812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6048
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...
1
5372
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
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...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
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

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.