• How to force the UI to refresh immediately(WPF)


    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.

        • 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.

      • 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

  • 相关阅读:
    PHP合并数组array_merge函数运算符加号与的区别
    PHP 字符串为空
    50段超实用CSS代码(1)
    WCF 第三章 信道 总结
    WCF 第三章 信道 操作契约和信道形状
    WCF 第三章 信道
    WCF 第三章 信道形状
    WCF 第二章 契约 数据契约版本
    WCF 第二章 契约 消息契约
    WCF 第二章 契约 系列文章
  • 原文地址:https://www.cnblogs.com/binbinxiong/p/3751183.html
Copyright © 2020-2023  润新知