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

  • 相关阅读:
    cloudera cdh4 hue 安装
    设计模式Observer(观察者模式)
    编译android源码三(编译系统)
    centos 6.3 修改默认的系统语言
    Linux下查看文件和文件夹大小的df和du命令
    编译android源码二(下载源代码)
    devenv.exe
    Javascript和xml合用
    DataKeys的用法
    XSL(转)
  • 原文地址:https://www.cnblogs.com/binbinxiong/p/3751183.html
Copyright © 2011-2022 走看看