Force redraw before long running operations

10,592

The following code will do what you're looking for. However I would not use it. Use the BackgroundWorker class for long time operations. It's easy to use and very stable.
Here 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 on how to use the BackgroundWorker.

Share:
10,592
Maestro
Author by

Maestro

Updated on July 26, 2022

Comments

  • Maestro
    Maestro almost 2 years

    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 grayed, because the long operation prevents the UI thread from repainting the control. I know the right design is to start a background thread / dispatcher, but sometimes that's too much hassle for a simple operation.

    So how do I force the button to redraw in disabled state? I tried .UpdateLayout() on the Button, but it didn't have any effects. I also tried System.Windows.Forms.DoEvents() which normally works when using WinForms, but it also had no effect.

  • HCL
    HCL over 12 years
    This will not work. Invalidation does only invalidate the region to be repainted. However it does not the painting.
  • edvaldig
    edvaldig over 12 years
    @HCL, Works fine for me, see the documentation for the function: Invalidates the rendering of the element, and forces a complete new layout pass. OnRender is called after the layout cycle is completed.
  • edvaldig
    edvaldig over 12 years
    He was pretty clear about being aware of but not using a Dispatcher/Backgroundworker :)
  • HCL
    HCL over 12 years
    Are you shure your UI-thread is blocked? I have tested it the last time under .net3.5SP1. At this time it has not worked. If it works now, they must have changed the behaviour with 4.0. However, this would be a big surprise to me.
  • HCL
    HCL over 12 years
    @edvaldig: I have underlined not to use it, however I have given him the code he was looking for. I don't think that this is worth a -1 from you. If my code does not work, then give me a -1, but not for giving some additional advice. I don't think this is fair.
  • edvaldig
    edvaldig over 12 years
    You're right, I was too quick about it, comment upvote to compensate :)
  • Eric Ouellet
    Eric Ouellet over 10 years
    How do you use BackgroundWorker thread if you want to open another application through COM (which I think should be done on the UI thread / STA Apartment) ?
  • Eric Ouellet
    Eric Ouellet over 10 years
    Does reactive solve the problem of long operation on COM which should be done on the UI thread (STA apartment) ?