0

I have a page that runs a large amount of code to prepare the page and layout of the page for the user.

It only needs to be used once in a blue moon. But when it does, it's about three seconds of wait for the user.

Apparently, Microsoft only allows a 2 or less second wait. Daaaaang.

I've optimized and optimized the code, and there's no way around it: the code is as fast as it can be. So naturally, I set out to make a loader.

I tried to update several controls, but thanks to the UI thread being frozen, they didn't work.

I searched and searched and searched, and found this, which was confusing and didn't really help, partly because of WP7's version of C# and partly because of me being a beginner at C#.

How can I make a loader that works while the UI thread is frozen?

Community
  • 1
  • 1
JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47

2 Answers2

2

The Silverlight Toolkit For Windows Phone introduces the PerformanceProgressBar, "which uses the compositor thread exclusively for animation, instead of the UI (user interface) thread" as described in this WindowsPhone Geek article. This should help you with the freezing UI.

As of the parallel execution (suggested by Merlyn), I'd suggest the QueueUserWorkItem method to execute code in a separate thread over the BackgroundWorker, leaving the UI thread active for UI updates, but as described in the article, you will need to use Dispatcher.BeginInvoke to update your UI from there.

thmshd
  • 5,729
  • 3
  • 39
  • 67
  • 1
    Thanks! I finally have a progress bar, even if it does display a little slowly on the emulator and doesn't show how the % done. Also, I boosted your rep into the 4-digit zone! – JavaAndCSharp Dec 11 '11 at 18:16
1

You can try using the BackgroundWorker class. That page has a tutorial for how to use it.

See this question and my answer for another tutorial. Though not Windows Phone 7 specific, it still has to do with updating the UI while waiting for a task to complete:

Here's a Windows Phone 7 specific tutorial for using the BackgroundWorker:

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I tried this several times, with several different configurations, and always got some sort of error or other. When using some code from the MSDN page and your previous answer, I got a message saying "System.UnauthorizedAccessException was unhandled" and "invalid cross thread access". Fortunately, I was able to use Thomas's answer above to salvage some sort of a loader. Thanks anyway; your answer opened my mind to the world of the BackgroundWorker. – JavaAndCSharp Dec 11 '11 at 18:14
  • 1
    @JavaAndCSharp: You were trying to update the UI in the background thread. You need to use the `Dispatcher` class to post updates to your UI. See: http://stackoverflow.com/questions/1924408/invalid-cross-thread-access-issue – Merlyn Morgan-Graham Dec 11 '11 at 20:09
  • 1
    @JavaAndCSharp: The advantage of the `BackgroundWorker` over other options is if you have a long running task, and you want to support cancellation, it already has a built-in mechanism for it. See: http://stackoverflow.com/questions/7731610/cancel-threadpool-queueuserworkitem-task - you can see from that question that you have to "roll your own" for the cancellation if you use `QueueUserWorkItem`. If your task isn't that long running, and you never want to cancel it, don't worry about it tho :) – Merlyn Morgan-Graham Dec 11 '11 at 20:15
  • 1
    Yeah, my task isn't that long-running, and I can't foresee any reason to cancel it. – JavaAndCSharp Dec 11 '11 at 20:25
  • 1
    @JavaAndCSharp: Then the other option is probably less code to implement. Cheers! – Merlyn Morgan-Graham Dec 11 '11 at 20:26