0

First, sorry for my english.

I'm making a WPF/C# application, with a Frame and pages.

When I try to change quickly the page displayed, by 4 or 5 times, the program starts to freeze and I need to wait.

I'm forced to use a Dispatcher to make the pages loading in background, so they'll not freeze the entire application. But still freezing. It seems that the dispatcher is not invoking a new thread.

I'm using the following code:

this.Dispatcher.Invoke((Action)(() =>         // BeginInvoke also don't work
{
    Page1 p = new Page1();
    Frame1.NavigationService.RemoveBackEntry();
    Frame1.Content = p;
}));

and still freezing! Any help?

Many thanks.

Guilherme
  • 5,143
  • 5
  • 39
  • 60

1 Answers1

3

I'm forced to use a Dispatcher to make the pages loading in background

That's not forcing them to load on a background thread, it's forcing them to load on the dispatcher's thread.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • I'm invoking a dispatcher because it's impossible to make a simple thread to work on the UI (http://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it). So, how can I make my application not freezing in this situation? – Guilherme Mar 13 '13 at 19:43
  • @Guilherme If your application is freezing then chances are you're not just updating the UI. You should separate your work into what is modifying the UI and what is not. Do the work that's not updating the UI (getting data from the database, reading/writing to files, doing processing on the CPU, etc) in a background thread. Then, when you're done with all of that, take the results and update the UI with them. That's how *all* UI-based programming ought to be done. – Servy Mar 13 '13 at 19:45
  • @Servy Yeah, but it's a blank page, without content and without code. I don't know why it's freezing my entire application. Well, there is no way to separate the load process of the page from the UI? – Guilherme Mar 13 '13 at 19:50
  • @Guilherme There's no magic button that you just press once that makes all UI code run in the UI thread and all non-UI code run in a background thread. *You* need to be the one to determine which code runs where, as it's not a problem that can be automated. – Servy Mar 13 '13 at 19:55
  • @Servy Yes, I understand that. I know that is not trivial, but there's NO code that I can put in background. There is no DataAccess, there is NO Page_loaded. I make NO code behind, and still freezing a lot. This: "Frame1.Content = p;" needs about one second to process. I only want this line in background. – Guilherme Mar 13 '13 at 20:15
  • @Guilherme If it needs ~1 second to process, it's doing *something*. Either you're updating the UI with huge amount of visual items, which will freeze your application for a second and there's nothing you can do about it, or you're effectively doing some kind of busy work on the UI thread. If it's the latter, do the preprocessing in the background thread and update the UI thread when you're done. – Patryk Ćwiek Mar 13 '13 at 20:51