开发者

Should I thread this?

I've got a "Loading Progress" WPF form which I instantiate when I start loading some results into my Main Window, and every time a result is loaded I want the progress bar to fill up by x amount (Based on the amount of results I'm loading).

However what happens is that the Progress bar in the window stays blank the entire time, until the results have finished loading, then it will just display the full progress bar.

Does this need threading to work properly? Or is it just to do with the way I'm trying to get it to work?

//code snippet
LoadingProgress lp = new Lo开发者_运维问答adingProgress(feedCount);
lp.Show();
foreach (FeedConfigGroup feed in _Feeds) {
  feed.insertFeeds(lp);
}

//part of insertFeeds(LoadingProgress lbBox)
foreach (Feeds fd in _FeedSource) {
    lpBox.setText(fd.getName);
    XmlDocument feedResults = new XmlDocument();
    feedResults.PreserveWhitespace = false;
    try {
        feedResults.Load(wc.OpenRead(fd.getURL));
    } catch (WebException) {
        lpBox.addError(fd.getName);
    }
    foreach (XmlNode item in feedResults.SelectNodes("/rss/channel/item")) {
      //code for processing the nodes...
    }
    lpBox.progressIncrease();
}

If more code is needed let me know.


Consider accessing the progressbar from the external thread thru a delegate asynchronously using the ProgressBar's Dispatcher.Invoke.

This post might be helpful.


I suggest you to use another thread to do the processing. The progress bar is filled at the end, so you didn't make a mistake on its programming.
In general, you should avoid to do processing in the user interface thread.


So, how many feeds and how many items? I'll wait for you to count them in order to set the total length of the progress bar.

...

What was that? You wouldn't know the total until after most of the work is already done? Well, sounds like the progress bar isn't the correct control to use.

You're doing (at least what should be) asynchronous work here. Definitely do it in a worker thread. Definitely let the user know work is going on in the background. But don't use a progress bar. You can't know exactly what point in the process you are at and how much longer you have to go until all the long-lasting processes have completed. Most of your users will see that your progress bar as the lie it is and will laugh at your suck.

Just use the standard "work is going on in the background" fadey-circle-thingy on your RSS feed box (an Adorner would work nicely) and be done with it.


Using this video I managed to get it working, Dispatch.BeginInvoke (Except for one which needed to be Dispatcher.Invoke) was the way to go. If anyone wants the final code let me know and I can post it up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜