J
John Kraft
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
{
}
}
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
{
}
}