HttpWebRequest file download and progress bar update kills main thread

  • Thread starter George Chatzigeorgiou
  • Start date
G

George Chatzigeorgiou

Hi all.

I'm having this problem:

My app , reads an xml file through Http , parses it for file version
differences with the local files, and starts downloading newer files.
This propably takes some time, so I want to show some progress
information to the user before he starts pressing every available button
on the device. I wish, that is. Because trying every available
configuration and combination but to no avail. Let me explain.

Option 1: Create an HttpWebRequest, an HttpWebResponse, and start
reading blocks of data. After every block , update the progress bar.
Result: Not only the progress bar doesn't update (repaint, refresh is
somewhat chocked by the Read method ), but after the first download
finished, the main thread exits. I take care to close everything after
the end of the download, but the problem persists.

Option 2: Use asynchronous transfer, and Control.Invoke for updates.
(same as the MSDN sample).
Result: Progress bar updates, but after the first operation is
completeted, guess what: main thread dies. Moreover there is no way to
control the operation (I need to replace the old file, but I wouldn't
know when the async. operation has finished....so option 3).

Option 3: Use asynchronous operations, and ManualResetEvent singaling to
control the operation.
Result: If I try to Invoke, everything dies. If I don't Invoke (for
progress bar update), after the first operation has finished, the main
thread dies.

It seems, that somehow, someone wants to delete a thread , and because
no other than the main thread is available, the app exits. I tries a
really big delay (like Thread.Sleep(1000000)) to give time to the next
webResponse to fire, and it worked until the next progress bar update
crashed everything again.

I'm sure I'm doing something terribly wrong, an RTFM somewhere in the
last page or whatever.
Any ideas?

Thanks in advance.
 
A

Alex Feinman [MVP]

What is in your main thread that dies after the first run? Are you using
WIndows Application model (main form that is instantiated via
Application.Run) or did you put together something of your own?

If the MSDN article that you refer to is this one[1], are you following the
model I use in that article or do you have something quite different?

1. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/AutoUpdater.asp
 
D

Daniel Moth

When you say the thread dies do you mean your application exits or it hangs?
If you share some code that replicates this problem we may have better
ideas...

A couple of results on a quick search may be of help:
http://groups-beta.google.com/group...est+thread&qt_g=1&searchnow=Search+this+group

For your scenario, the BackgroundWorker may be suitable (usually is when a
background operation needs to report progress to the UI):
http://www.danielmoth.com/Blog/2004/12/backgroundworker-for-cf-10.html

Cheers
Daniel
 
G

George Chatzigeorgiou

Alex said:
What is in your main thread that dies after the first run? Are you using
WIndows Application model (main form that is instantiated via
Application.Run) or did you put together something of your own?

If the MSDN article that you refer to is this one[1], are you following
the model I use in that article or do you have something quite different?

1. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/AutoUpdater.asp

Hi Alex.
Thanks a lot for the quick response.

Yes, the main thread that dies comes from Application.Run.
And when I say die, I mean that is is killed.
If the app is executed from withing the VS debugger, then I get a remote
connection lost error, and if I'm just executing the app normally, it
just vanishes (actually it is killed, it doesn't appear in the task
manager).

I've used a hybrid of the code the MSDN article uses, because I need to
update a number of files and not just one. For a single file, everything
works fine, but if you want to start a new download after the first has
finished, you must have some sort of control over the operation.
I used ManualResetEvent.WaitOne, and ManualResetEvent.Set when the last
byte was recieved. But as I mentioned in the first post, this didn't
work, as when I used the BegineRead function again, together with invoke
the thread was killed somehow ( but I did managed to pass through
this, by setting a Thread.Sleep(OneCentury) , only to get a glorious
hang in the next pgProgress.Invoke() ).

What I'm actually trying to do is this:

Download a list files that need to be updated.
Start downloading the files one by one, while updating two progress
bars, one reporting total progress and the other one current file
progress. I still believe I'm missing something trivial here.

Anyway, I can provide you with some snippets, when I get back to the
office.

Thanks for the help anyway.
George Chatzigeorgiou
 
G

George Chatzigeorgiou

Daniel Hi.

Daniel said:
When you say the thread dies do you mean your application exits or it
hangs? If you share some code that replicates this problem we may have
better ideas...

Yeap, I'll try to provide some code.
It just needs some cleaning.
For your scenario, the BackgroundWorker may be suitable (usually is when
a background operation needs to report progress to the UI):
http://www.danielmoth.com/Blog/2004/12/backgroundworker-for-cf-10.html

That's very interesting Daniel.
I'll have a look.

Thanks again for the reply
George Chatzigeorgiou
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top