Page 1 of 1

How to force UI update before blocking operation? [SOLVED]

Posted: Sun Mar 20, 2016 5:37 am
by rkmore
I have an animated GIF that I want to set to "Visible" right before I do a fairly long blocking operation.

When I do then it does not show (If I don;t do the blocking operation it shows fine).

Is there a method to cause the UI to update immediateld? A DoEvents sort of thing?

Code: Select all

                Working.Visibility = Visibility.Visible;

                Sha2.Sha256 hasher = new Sha2.Sha256();
                byte[] array = Encoding.ASCII.GetBytes(cPassword.Password + salt);
                hasher.AddData(array, 0, (uint)array.Length);
                uint[] hash = hasher.GetHashUInt32();
                StringBuilder sb = new StringBuilder();
                foreach (uint i in hash) sb.Append(i.ToString("X04"));

                App.cCollector = App.cServiceClient.GetCollectorForUserName(cUsername.Text);

Re: How to force UI update before blocking operation?

Posted: Sun Mar 20, 2016 11:27 am
by JS-Support @Userware
Hi,

Yes, this is possible.

All you need to do is to move your long blocking operation into a Dispatcher.BeginInvoke(...) call.

Here is an example:

Code: Select all

//Here you update the UI

Dispatcher.BeginInvoke(() => {

  // Here you do the long blocking operation. This code will be executed on the UI thread as soon as the UI thread is free.

});

// The code you put here will be executed BEFORE the long blocking operation, and also BEFORE the UI update.

// As soon as this thread is finished, the UI gets updated.


You can find more information about the Dispatcher.RunAsync method online.

Thanks.
Regards,
JS-Support

Re: How to force UI update before blocking operation?

Posted: Mon Mar 21, 2016 3:30 am
by rkmore
That was what I tried first. I get....

Code: Select all

Error   CS1061   'CoreDispatcher' does not contain a definition for 'RunAsync' and no extension method 'RunAsync' accepting a first argument of type 'CoreDispatcher' could be found (are you missing a using directive or an assembly reference?)   


And I can find no reference that allows it. I am under 7.2

RKM

Re: How to force UI update before blocking operation?

Posted: Mon Mar 21, 2016 9:46 am
by JS-Support @Userware
Sorry, the method name is "BeginInvoke", not "RunAsync". I have updated the previous post. Please let me know if it works.

By the way, you can find the reference of the CoreDispatcher at the following URL:
http://help.cshtml5.com/html/806f0ccc-ebed-0142-38b7-15e84693477a.htm

Regards,
JS-Support