UI Element is not updating

William

If you can picture this as a loading bar:

for (int i = 0; i < r.Width / 2; i++) //Fills half the loading bar.
{         
    Rectangle rect = can.Children[3] as Rectangle; //getting the 3rd element of my Canvas
    if (rect != null)
     {
         rect.Fill = brush;
         rect.Width = i;
         Thread.Sleep(50);
     }
}

So, the application will not show the progress of adding additional rectangles. Instead it just freezes up for the total time of the thread sleep and when the for loop is finished it will show the entire bar.

Why is this?

I'm aware there is a loading bar built in, but this is just an example of problems I have been having.

Sangram Nandkhile

I'd recommend you start reading about Threads, background workers, dispatchers. Unless you add threads to your solution, the entire applications runs with the help of UI thread.

If you have got task which can be performed serially, you need not implement Threads or background worker. In your case, you are trying to do 2 task.

  1. Update the rectangle and draw it on window
  2. Sleep thread for 50 milliseconds

When UI thread executes thread.Sleep(50); it cannot draw (show progress in rectangle). That's the reason your application kind of freezes for seconds and gets completed before you could see any progress. I am going to try and explain this with 2 examples.

Example:1 Add a progress bar with IsIndeterminate="True" and Button1 to your demo application.

<ProgressBar IsIndeterminate="True"  Height="20" Name="progressBar1" Width="177" Visibility="Hidden" />

In code behind,

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            progressBar1.Visibility = Visibility.Visible;
            Thread.Sleep(5000);
            progressBar1.Visibility = Visibility.Hidden;
        }

If you run this code, You will not see any progress bar as the code is getting executed serially. To make sure that progressbar is getting shown and hide properly, do following changes.

Example 2:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            progressBar1.Visibility = Visibility.Visible;
            Thread t = new Thread(DoMyWork);
            t.Start();
        }

   private void DoMyWork()
                {
                    Thread.Sleep(5000);
                    //hide the progress bar after completing your work
                    this.Dispatcher.Invoke((Action)delegate { progressBar1.Visibility = Visibility.Hidden; });
                }

enter image description here

Run this code and you can see that while UI thread updates progressbar, new thread created executes DoMyWork function.

You will need this.Dispatcher.Invoke((Action)delegate as progressBar1 was created in UI thread and you cannot access it in a another thread. i.e.Thread t = new Thread(..)

P.S.- This is dirty of way to coding. It is just to give an idea that how things should work in OP's case.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Tree Node Id not updating. Element UI Vue

分類Dev

UI not updating on some devices

分類Dev

Flutter provider not updating the UI

分類Dev

why is my HandlerThread updating the UI

分類Dev

Updating UI using ViewModel and DataBinding

分類Dev

UI-grid data is not updating

分類Dev

Updating array element in a collection in Meteor

分類Dev

AutoLayout In Side UI Element

分類Dev

JSON parsing with SwiftyJSON - Trouble updating UI

分類Dev

Calculated field not updating until edited in UI

分類Dev

Angular component is not updating UI when value changes

分類Dev

Updating UI options from reactivePoll in shiny Server

分類Dev

Updating Activity UI from Intent Service?

分類Dev

Angular UI Router is not updating the layout view

分類Dev

Change of element in column not updating in data frame

分類Dev

How to preview image in element ui?

分類Dev

Angularjs 2 - Updating template or component does not update the UI

分類Dev

xcode to crash due to reading when updating UI textField

分類Dev

Android: Updating the UI from an adapter inside a While Loop

分類Dev

Named view in angular ui-router not updating, despite being watched

分類Dev

Updating UI from a background Thread and thread within a Thread

分類Dev

Angular updating $scope variable after ajax is not reflecting in UI

分類Dev

Meteor.js collection: new record not updating in bootstrap ui popover

分類Dev

Updating UI after multiple asynchronous completion handlers complete

分類Dev

Design UI Android (Element between CardViews)

分類Dev

element Ui: Carousel - How to set current slide

分類Dev

Semantic UI react size prop on image element

分類Dev

how to scroll a form to a specific UI element in SwiftUI

分類Dev

Jquery UI : Use slider for changing element opacity

Related 関連記事

  1. 1

    Tree Node Id not updating. Element UI Vue

  2. 2

    UI not updating on some devices

  3. 3

    Flutter provider not updating the UI

  4. 4

    why is my HandlerThread updating the UI

  5. 5

    Updating UI using ViewModel and DataBinding

  6. 6

    UI-grid data is not updating

  7. 7

    Updating array element in a collection in Meteor

  8. 8

    AutoLayout In Side UI Element

  9. 9

    JSON parsing with SwiftyJSON - Trouble updating UI

  10. 10

    Calculated field not updating until edited in UI

  11. 11

    Angular component is not updating UI when value changes

  12. 12

    Updating UI options from reactivePoll in shiny Server

  13. 13

    Updating Activity UI from Intent Service?

  14. 14

    Angular UI Router is not updating the layout view

  15. 15

    Change of element in column not updating in data frame

  16. 16

    How to preview image in element ui?

  17. 17

    Angularjs 2 - Updating template or component does not update the UI

  18. 18

    xcode to crash due to reading when updating UI textField

  19. 19

    Android: Updating the UI from an adapter inside a While Loop

  20. 20

    Named view in angular ui-router not updating, despite being watched

  21. 21

    Updating UI from a background Thread and thread within a Thread

  22. 22

    Angular updating $scope variable after ajax is not reflecting in UI

  23. 23

    Meteor.js collection: new record not updating in bootstrap ui popover

  24. 24

    Updating UI after multiple asynchronous completion handlers complete

  25. 25

    Design UI Android (Element between CardViews)

  26. 26

    element Ui: Carousel - How to set current slide

  27. 27

    Semantic UI react size prop on image element

  28. 28

    how to scroll a form to a specific UI element in SwiftUI

  29. 29

    Jquery UI : Use slider for changing element opacity

ホットタグ

アーカイブ