zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    angularjs中的jqlite的认识理解及操作使用
    轻谈Normalize.css
    oppo R9 WLAN使用代理图解
    前端小炒的win7使用笔记(收藏篇)
    git操作方便,简单使用的客户端sourcetree 安装配置所遇问题总结
    关于js中对事件绑定与普通事件的理解
    IT最新最火的网络词汇*2*(文章来源电商)
    Git笔记之初识vi编辑器
    番茄钟工作法--我们天生爱分享
    探讨"点"语法的奥秘
  • 原文地址:https://www.cnblogs.com/binbinxiong/p/3751183.html
Copyright © 2011-2022 走看看