views:

444

answers:

3

I'm processing large files after they are selected by the user. My code looks like the following:

if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

This freezes up the UI so I tried to display a loading message first before a user selected the file to give them a visual cue that something was happening:

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

Unfortunately, this still completely freezes up the UI while the file is being processed.

What I have found that works perfectly is if I fire a MessageBox right after the file selection. I think it does a "DoEvents" type call under the hood to get flush event/ui items in the runtime.

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) {
    MessageBox.Show("Sync!");
    // process really big file
}

In cases like this the big file is still processed as slowly but the loading message is displayed and the screen UI gets synched up (I'm doing some other things in the real thing such as showing a wait cursor).

Question:

Silverlight has no DoEvents functionality. Is there a call I can make besides MessageBox.Show to have the same effect of synchronizing the UI and preventing the OpenFileDialog from freezing up the UI?

+2  A: 

Use the BackgroundWorker class to process your really big file on a background thread? Here a link for you: How to: Use a BackgroundWorker

Doug Rohrer
+1  A: 

Use a BackgroundWorker to execute the heavy computation in the background. When you want to notify the UI that the operation is complete, the RunWorkerCompleted event of the aforementioned class can be used. The alternative to this is to use a completely separate thread and then callback to the UI thread using Dispatcher.BeginInvoke(). Remember that with .NET in a separate thread you cannot access any GUI components without this kind of synchronization.

Guy
A: 

Although Loading Bitmaps: DoEvents and the closure pattern is about WPF I found it helpful and the same ideas apply to Silverlight with only a few minor changes.