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

  • 相关阅读:
    DataAnnotations
    使用BizTalk实现RosettaNet B2B So Easy
    biztalk rosettanet 自定义 pip code
    Debatching(Splitting) XML Message in Orchestration using DefaultPipeline
    Modifying namespace in XML document programmatically
    IIS各个版本中你需要知道的那些事儿
    关于IHttpModule的相关知识总结
    开发设计的一些思想总结
    《ASP.NET SignalR系列》第五课 在MVC中使用SignalR
    《ASP.NET SignalR系列》第四课 SignalR自托管(不用IIS)
  • 原文地址:https://www.cnblogs.com/binbinxiong/p/3751183.html
Copyright © 2011-2022 走看看