I am new to C#. I have implemented a class, that works more or less like this:
- Download a web page
- Process contents
- Download some data
- Process the data
- And so on... (repeat x 10)
This works great when flow of control is sequential - I just need to create a new object, and in its constuctor all these steps are being performed sequentially, so when the object is created, it contains all necessary data stored in it. Each step depends on the previous one and there is no need to introduce any concept of parallelism:)
But in order to make my app look professional:) I would like to have a progress bar, which reports progress of each of these steps to the user. And this is when I learned that I can't really download anything with progress reporting without use of asynchronous operations (i.e. Async methods of WebClient).
I tried quite a lot of different things to get a method, that simply returns the data and is able to report progress back to the GUI:
1. WebClient + WaitHandle - tried to suspend main thread after running WebClient Async method. In this case callback method is never called and the app just hangs forever.
2. WebClient + custom thread semaphore - the same
3. WebClient + BeginInvoke and waiting on IAsyncResult - this actually produces some decent result, but I don't know how to return data from it
4. I even tried to use a C-like synchronous socket read loop. This one actually does something, but I can only see the progress bar filling itself after download is completed. DoEvents(), Update()'s, even decreasing progress bar gauge didn't help.
My question is: does anyone met with that kind of problem and knows how to solve it? I don't want to end up redesigning my class to be a bunch of self-calling event handlers, when there is no need to do so:)
Thanks in advance!