Forced redrawing before long operations

When you have a button and do something like:

Private Function Button_OnClick Button.Enabled = False [LONG OPERATION] End Function 

Then the button will not be gray because a lengthy operation prevents the user interface control from being redrawn. I know that the right design is to start the background thread / dispatcher, but sometimes it is too much trouble for a simple operation.

So, how to make the button redraw in the off state? I tried .UpdateLayout () on the button, but it had no effects. I also tried System.Windows.Forms.DoEvents (), which usually works when using WinForms, but it also had no effect.

+7
source share
3 answers

The following code will do what you are looking for. However, I would not use it. Use the BackgroundWorker class for lengthy operations. It is easy to use and very stable.
Here is the code:

  public static void ProcessUITasks() { DispatcherFrame frame = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) { frame.Continue = false; return null; }), null); Dispatcher.PushFrame(frame); } 

Here you will find a sample of how to use BackgroundWorker.

+8
source

InvalidateVisual (); @HCL is right ... don't do this

As you say, it is better to start using the background thread / dispatcher and unlock the user interface thread. Check out Microsoft's Reactive Extensions library for high-level asynchronous ui programming

0
source

In Windows.Forms, you can Button.Refresh() .

In Windows.Forms or WPF, you can submit messages to the pump to redraw. Async / Await was designed to allow you to do this without the muck of an HCL response.

 Private Async Function Button_OnClick Button.Enabled = False Await Task.Yield [LONG OPERATION] End Function 
0
source

All Articles