码迷,mamicode.com
首页 > 其他好文 > 详细

How to force the UI to refresh immediately(WPF)

时间:2014-05-25 22:19:32      阅读:657      评论:0      收藏:0      [点我收藏+]

标签:des   winform   class   c   code   tar   

Question

    • Folks,

      In my application, when the user hits "Submit" button, I have to make a Web service call asynchronously. Here is what I do.

      1. Disable a bunch of controls, update some text:

       

      this._btnSubmit.IsEnabled = false;
      
      ...
      this._lblInfo.Text = "Connecting..."
      
      

      2. Use Dispatcher.BeginInvoke for the actual Web service call.

       

      this.Dispatcher.BeginInvoke(DoTheStuff, DispatcherPriority.ContextIdle);
      
      

      The idea is to that the UI will get updated before the actual asynchronous call to the Web service is made.

      The problem is that the UI is not updating immediately. It takes a few seconds before the controls disable and the text appears.

      What is it that I need to do to ensure that the UI gets updated?

      I probably could spawn a background thread but that would be an overkill given that the Web service call is already asynchronous.

      Thank you in advance for your help.

      Regards,
      Peter

       

       

      Thursday, January 06, 2011 8:10 PM
       
 

Answers

    • Hi Peter Taps,

      Based on your description, I think you could try to below code snippet to achieve your goal(Force UI refresh).

       private delegate void NoArgDelegate();

       public static void Refresh(DependencyObject obj)

       {

                  obj.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,

                      (NoArgDelegate)delegate { });

       }

       private void Button_Click(object sender, RoutedEventArgs e)

       {

                   Refresh(testDtGrid);           

             

      }

       

      Best regards,


      Sheldon _Xiao [MSFT]
      MSDN Community Support | Feedback to us
      Get or Request Code Sample from Microsoft
      Please remember to mark the replies as answers if they help and unmark them if they provide no help.

      bubuko.com,布布扣
      • Marked as answer by Peter Taps Wednesday, January 12, 2011 7:27 PM
      Monday, January 10, 2011 4:00 AM
       
 

All replies

    • I know this doesn‘t answer your question, but have you considered using a BusyIndicatorto disable interaction with the window, instead of manually enabling and disabling all the controls.  Remember, this isn‘t WinForms.  The Extended WPF Toolkit has a free one.
      Thursday, January 06, 2011 8:41 PM
       
 
  • Hi Peter Taps,

    Based on your description, I think you could try to below code snippet to achieve your goal(Force UI refresh).

     private delegate void NoArgDelegate();

     public static void Refresh(DependencyObject obj)

     {

                obj.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,

                    (NoArgDelegate)delegate { });

     }

     private void Button_Click(object sender, RoutedEventArgs e)

     {

                 Refresh(testDtGrid);           

           

    }

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    bubuko.com,布布扣
    • Marked as answer by Peter Taps Wednesday, January 12, 2011 7:27 PM
    Monday, January 10, 2011 4:00 AM

 

_________________

 

for url: http://social.msdn.microsoft.com/Forums/vstudio/en-US/d651bd33-53fc-4218-9089-cd77d32f4fd8/how-to-force-the-ui-to-refresh-immediately?forum=wpf

How to force the UI to refresh immediately(WPF),布布扣,bubuko.com

How to force the UI to refresh immediately(WPF)

标签:des   winform   class   c   code   tar   

原文地址:http://www.cnblogs.com/binbinxiong/p/3751183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!