473,465 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange behavior when closing stream

Hello all,

I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk for further parsing. I
know, I could use the WebClient and it would be easier, but I don't
have the flexibility I want with it.

This code appears to work exactly the way I want unless the user
cancels the the background operation. In that case, when I call
Close() on the StreamReader object, the method just ends. Nothing
after the call to Close() get executed; which causes some bad side
effect. If I comment out the Reader.Close() line, it work perfectly.
However, I am worried that not closing that stream will cause me
problems somehow.

I have found that it isn't just when I call close, it's when I attempt
to read more data or anything else dealing with the stream. Properties
are ok to check, but methods cause the same behavior.

The code executes correctly if the cancel button is not clicked.

Any ideas or suggestions welcome.

Stats: XP, VS2005, SQLServer 2005

Thanks,

John

p.s. I'm new to .Net2 and threading in general. :)
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
MyPostVars vasr = e.Argument as MyPostVars;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(vars.ToPostString());

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://www.someCrazyWebsite/Download.do");
request.ContentLength = data.Length;
requ est.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

StreamReader reader = new
StreamReader(response.GetResponseStream());

System.IO.FileInfo file = new
System.IO.FileInfo("outputfile.txt");
if (file.Exists)
{
file.Delete();
}

StreamWriter writer = new StreamWriter(file.FullName);
writer.AutoFlush = true;

int count = 0;
int read = 0;
int size = 65536;
char[] buffer = new char[size];

while (!backgroundWorker.CancellationPending &&
!reader.EndOfStream)
{
count = reader.Read(buffer, 0, size);
read += count;
writer.Write(buffer, 0, count);
backgroundWorker.ReportProgress(0, read);
}

reader.Close(); <-------------------- Problem!!!!!!!!
writer.Close();

if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}

}
Oct 24 '06 #1
10 2311
John,

I don't know too much about the details of your stream -- they tend to
be idiosynchratic -- but try a

using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
{
...
}

block around your reader. You should be able to avoid the call to
Close(), which may solve your problem.

What do you expect to be happening after your Close() call anyway?
Looks like there may be some threading issues. Do you have a try/catch
block around the thread boundary of the background thread?
Stephan


John Kraft wrote:
Hello all,

I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk for further parsing. I
know, I could use the WebClient and it would be easier, but I don't
have the flexibility I want with it.

This code appears to work exactly the way I want unless the user
cancels the the background operation. In that case, when I call
Close() on the StreamReader object, the method just ends. Nothing
after the call to Close() get executed; which causes some bad side
effect. If I comment out the Reader.Close() line, it work perfectly.
However, I am worried that not closing that stream will cause me
problems somehow.

I have found that it isn't just when I call close, it's when I attempt
to read more data or anything else dealing with the stream. Properties
are ok to check, but methods cause the same behavior.

The code executes correctly if the cancel button is not clicked.

Any ideas or suggestions welcome.

Stats: XP, VS2005, SQLServer 2005

Thanks,

John

p.s. I'm new to .Net2 and threading in general. :)
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
MyPostVars vasr = e.Argument as MyPostVars;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(vars.ToPostString());

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://www.someCrazyWebsite/Download.do");
request.ContentLength = data.Length;
requ est.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

StreamReader reader = new
StreamReader(response.GetResponseStream());

System.IO.FileInfo file = new
System.IO.FileInfo("outputfile.txt");
if (file.Exists)
{
file.Delete();
}

StreamWriter writer = new StreamWriter(file.FullName);
writer.AutoFlush = true;

int count = 0;
int read = 0;
int size = 65536;
char[] buffer = new char[size];

while (!backgroundWorker.CancellationPending &&
!reader.EndOfStream)
{
count = reader.Read(buffer, 0, size);
read += count;
writer.Write(buffer, 0, count);
backgroundWorker.ReportProgress(0, read);
}

reader.Close(); <-------------------- Problem!!!!!!!!
writer.Close();

if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}

}
Oct 24 '06 #2
John Kraft wrote:
I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk for further parsing. I
know, I could use the WebClient and it would be easier, but I don't
have the flexibility I want with it.

This code appears to work exactly the way I want unless the user
cancels the the background operation. In that case, when I call
Close() on the StreamReader object, the method just ends. Nothing
after the call to Close() get executed; which causes some bad side
effect.
That sounds like an exception is being thrown.

1) You should work out your general exception handling mechanisms so
that you get to see that exception
2) The exception is likely to provide information which will help you
understand why it's happening.

Jon

Oct 24 '06 #3
On 24 Oct 2006 08:57:45 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com>
wrote:
>John Kraft wrote:
>I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk for further parsing. I
know, I could use the WebClient and it would be easier, but I don't
have the flexibility I want with it.

This code appears to work exactly the way I want unless the user
cancels the the background operation. In that case, when I call
Close() on the StreamReader object, the method just ends. Nothing
after the call to Close() get executed; which causes some bad side
effect.

That sounds like an exception is being thrown.

1) You should work out your general exception handling mechanisms so
that you get to see that exception
2) The exception is likely to provide information which will help you
understand why it's happening.

Jon

Thanks for the input, Jon.
I've got a try/catch around the entire inside of the DoWork method,
which from what I've read over the past several days is what you're
supposed to do. No exception "appears" to be thrown; that is, it
never stops inside the catch block. It's as if the thread just ceases
to exist. I've also tried wrapping the individual call to the Close
method inside a try/catch (for troubleshooting purposes) with no
luck. I don't know where else I could put a try/catch to catch such
an exception.

The part that really "creeps" me out is that it only seems to occur if
I try to act on the streamreader. If I comment out the call, the
function terminates normally.
Oct 24 '06 #4
On 24 Oct 2006 08:37:58 -0700, "ssamuel" <ss*****@gmail.comwrote:
>John,

I don't know too much about the details of your stream -- they tend to
be idiosynchratic -- but try a

using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
{
...
}

block around your reader. You should be able to avoid the call to
Close(), which may solve your problem.

What do you expect to be happening after your Close() call anyway?
Looks like there may be some threading issues. Do you have a try/catch
block around the thread boundary of the background thread?
Stephan
Thanks for the reply, Stephan.

I just tried that possibility. What I discovered is that when the
compiler gets to the closing brase of the using statement, the method
terminates exactly the way it did with the Close method.

What I expect to happen, is for the method to complete execution. Out
of habit, I always close my readers before i close my writers. In
this case, the premature exiting of the method causes the
writer.Close() and subsequent instructions to not execute. The has
the side effect of keeping the output file "locked" until I exit the
application. This causes me to be unable to perform the necessary
parsing of the files at a later point in the application.

Aside from those effects, I don't want to be hogging up system
resources anywhere by having an open stream to a server on the
internet; especially if users decide to cancel the download and
restart it at a later time.

Thanks,

John
Oct 24 '06 #5
John,

First, in this case, you probably should be closing the writer before
the reader. Based on what your code looks like, you're using the writer
to re-write what came in from the reader; therefore, the writer depends
on the reader and should always be closed first. If your "always reader
before writer" philosophy came from a rule of thumb, you should
consider that this may be one of (possibly few) times when it doesn't
apply.

Second, what's going on in the background thread? Are you manipulating
the same streams or otherwise related objects? Are you catching
exceptions at the boundaries of that thread? Very often, when things go
silently into the night, it's because you're throwing exceptions in
background threads. There's no exception handling on them and they're
not in the process foreground so you'll rarely hear from them when they
crash.

Stephan

John Kraft wrote:
Thanks for the reply, Stephan.

I just tried that possibility. What I discovered is that when the
compiler gets to the closing brase of the using statement, the method
terminates exactly the way it did with the Close method.

What I expect to happen, is for the method to complete execution. Out
of habit, I always close my readers before i close my writers. In
this case, the premature exiting of the method causes the
writer.Close() and subsequent instructions to not execute. The has
the side effect of keeping the output file "locked" until I exit the
application. This causes me to be unable to perform the necessary
parsing of the files at a later point in the application.

Aside from those effects, I don't want to be hogging up system
resources anywhere by having an open stream to a server on the
internet; especially if users decide to cancel the download and
restart it at a later time.

Thanks,

John
Oct 24 '06 #6
On 24 Oct 2006 10:53:45 -0700, "ssamuel" <ss*****@gmail.comwrote:
>John,

First, in this case, you probably should be closing the writer before
the reader. Based on what your code looks like, you're using the writer
to re-write what came in from the reader; therefore, the writer depends
on the reader and should always be closed first. If your "always reader
before writer" philosophy came from a rule of thumb, you should
consider that this may be one of (possibly few) times when it doesn't
apply.

Second, what's going on in the background thread? Are you manipulating
the same streams or otherwise related objects? Are you catching
exceptions at the boundaries of that thread? Very often, when things go
silently into the night, it's because you're throwing exceptions in
background threads. There's no exception handling on them and they're
not in the process foreground so you'll rarely hear from them when they
crash.

Stephan
Stephan,

The reader first thing was just something I've always done. No real
"logic" behind it. I think I just always do that because it helps me
to remember too. But, your reasoning makes plenty o' sense. However,
with the using statement, I am closing the writer first with no change
in the wierd behavior.

As for the threading, the DoWork method IS the background thread.
According to the threading books I've been reading, it is a
recommended practice to put a try as the first line of the DoWork
method and wrap all execution inside the try/catch. Nothing is being
caught inside that catch block, but I realize that doesn't mean it's
not being thrown somewhere deeper.

As for manipulation of the streams, all of it takes place inside that
one thread. They are created, used, and destroyed in that one method;
which is running in it's own thread.

Thanks,

John
Oct 24 '06 #7
Hi John,

is it possible, that the MessageBox.Show throws an exception?
This would explain, why the exception is lost totally. Maybe there is a
problem with MessageBox.Show in a Background-thread.
Did you step through the application in the debugger? Then it stopp at least
at the Show statement (if my assumption is true.)

"John Kraft" <jh*****@barbecueguy.comschrieb im Newsbeitrag
news:uk********************************@4ax.com...
Hello all,

I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk for further parsing. I
know, I could use the WebClient and it would be easier, but I don't
have the flexibility I want with it.

This code appears to work exactly the way I want unless the user
cancels the the background operation. In that case, when I call
Close() on the StreamReader object, the method just ends. Nothing
after the call to Close() get executed; which causes some bad side
effect. If I comment out the Reader.Close() line, it work perfectly.
However, I am worried that not closing that stream will cause me
problems somehow.

I have found that it isn't just when I call close, it's when I attempt
to read more data or anything else dealing with the stream. Properties
are ok to check, but methods cause the same behavior.

The code executes correctly if the cancel button is not clicked.

Any ideas or suggestions welcome.

Stats: XP, VS2005, SQLServer 2005

Thanks,

John

p.s. I'm new to .Net2 and threading in general. :)
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
MyPostVars vasr = e.Argument as MyPostVars;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(vars.ToPostString());

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://www.someCrazyWebsite/Download.do");
request.ContentLength = data.Length;
requ est.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

StreamReader reader = new
StreamReader(response.GetResponseStream());

System.IO.FileInfo file = new
System.IO.FileInfo("outputfile.txt");
if (file.Exists)
{
file.Delete();
}

StreamWriter writer = new StreamWriter(file.FullName);
writer.AutoFlush = true;

int count = 0;
int read = 0;
int size = 65536;
char[] buffer = new char[size];

while (!backgroundWorker.CancellationPending &&
!reader.EndOfStream)
{
count = reader.Read(buffer, 0, size);
read += count;
writer.Write(buffer, 0, count);
backgroundWorker.ReportProgress(0, read);
}

reader.Close(); <-------------------- Problem!!!!!!!!
writer.Close();

if (backgroundWorker.CancellationPending)
{
e.Cancel = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}

}

Oct 25 '06 #8
John Kraft <jh*****@barbecueguy.comwrote:
I've got a try/catch around the entire inside of the DoWork method,
which from what I've read over the past several days is what you're
supposed to do. No exception "appears" to be thrown; that is, it
never stops inside the catch block. It's as if the thread just ceases
to exist. I've also tried wrapping the individual call to the Close
method inside a try/catch (for troubleshooting purposes) with no
luck. I don't know where else I could put a try/catch to catch such
an exception.

The part that really "creeps" me out is that it only seems to occur if
I try to act on the streamreader. If I comment out the call, the
function terminates normally.
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 Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 25 '06 #9
On Wed, 25 Oct 2006 21:36:09 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>John Kraft <jh*****@barbecueguy.comwrote:
>I've got a try/catch around the entire inside of the DoWork method,
which from what I've read over the past several days is what you're
supposed to do. No exception "appears" to be thrown; that is, it
never stops inside the catch block. It's as if the thread just ceases
to exist. I've also tried wrapping the individual call to the Close
method inside a try/catch (for troubleshooting purposes) with no
luck. I don't know where else I could put a try/catch to catch such
an exception.

The part that really "creeps" me out is that it only seems to occur if
I try to act on the streamreader. If I comment out the call, the
function terminates normally.

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,

I appreciate all the help. I'll see if I can come up with something.
The reason I didn't already post the complete code is that the
information that I'm sending is sensitive information that is posting
against a U.S. government website. Even if I were to give you the
entire program, you wouldn't be able to perform the download without
the post data; which I'm not at liberty to give out. I think maybe
this week/weekend I'll see if I can reproduce the issue downloading a
file against some other website or even just a simple filestream. If
I can, then I'll post the program with that code.

Thanks again,

John
Oct 25 '06 #10
On Wed, 25 Oct 2006 16:25:12 +0200, "Christof Nordiek" <cn@nospam.de>
wrote:
>Hi John,

is it possible, that the MessageBox.Show throws an exception?
This would explain, why the exception is lost totally. Maybe there is a
problem with MessageBox.Show in a Background-thread.
Did you step through the application in the debugger? Then it stopp at least
at the Show statement (if my assumption is true.)
Christof,

That's funny, I didn't even realize I left that MessageBox in there
when I posted the sample. That's why I had it there, I just needed a
line of code for the debugger to stop on. The debugger never even
reaches the line of code. It never enters the catch block.

Thanks though,

John
Oct 25 '06 #11

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

Similar topics

2
by: swp | last post by:
I have a site created using ASP, HTML, and JavaScript. I use frames to manage a few things, and requires SSL to connect. The site is on a secured network, not meant to be cross-browser...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click...
4
by: Abhishek Srivastava | last post by:
Hello All, I wrote a program in which I have a OleDbConnection which I open in the contructor and close in the Destructor. When I run the program I get the following error Unhandled...
0
by: psg | last post by:
Hello! I was going through help topics under IDE. Once when I tried to close Help tab it did not close, but it opened itself in a new tab. So I got two Help tabs with the same content. When I...
1
by: hansolox1 | last post by:
The following program simply sets the icon of a form called form1. When I get the name of the embedded icon using GetManifestResourceNames(), I store the name in a string variable called s. The...
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
3
by: John | last post by:
In my app I've created a label report (Avery 7162, 2 colums, 8 rows) with the wizzard. The report is based on a temp table that is created when the user pushes a button. After creation of the table...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
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,...
0
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
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
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 ...

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.