|
-
December 14th, 2011, 10:00 AM
#1
Using DoEvents in WPF
Someone on one of the forums got me started in updating a TextBlock control on demand by using a created version of DoEvents(), which isn't supported by WPF. It works, but doesn't update on the 2nd call. Can you not use a DoEvents() call twice? Before executing my 'Get_Removable_Drives()' the control is updated. When the drive list appears, the control is not updated (hidden). What else do I need to do?
void Get_Removable_Drives()
{
RemovableDrives RDwindow = new RemovableDrives();
RDwindow.ShowDialog();
}
public static void DoEvents()
{ Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background,
new EmptyDelegate(delegate{}));
}
void Select_Click(object sender, RoutedEventArgs e)
{
TBStatus.Foreground = Brushes.Red;
TBStatus.Visibility = Visibility.Visible; // Puts 'Please Wait'
DoEvents();
Get_Removable_Drives();
TBStatus.Visibility = Visibility.Hidden; // Hides 'Please Wait'
DoEvents();
}
-
December 14th, 2011, 01:11 PM
#2
Re: Using DoEvents in WPF
This really isn't the way to do things. DoEvents or anything like it should be avoided at all costs because it can cause all sorts of problems (re-entrancy of code etc etc).
What you really should be doing is having a separate thread which your long-running task is on.
The sequence of operation should be something like :
1. Display "Please wait"
2. Disable all controls relevant.
3. Fire thread
4. Thread starts long running task
5. When finished thread signals back to UI that it's finished.
6. UI hides "Please wait".
Have a look at the BackgroundWorker class which simplifies a lot of this stuff for you.
There are loads of pages on the net to help too. Have a google for "WPF asynchronous method" or something like.
Yours,
Darwen.
-
December 14th, 2011, 01:22 PM
#3
Re: Using DoEvents in WPF
Thanks. I'll try that approach, too. I did get it working properly using my created DoEvents() method. I like the BackgroundWorker approach. It sounds more logical.
Sutton
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|