How to change TextBlock.Text every loop?











up vote
3
down vote

favorite












This is UWP C# application. I want to display the i variable.



    private void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
}
}


But after I clicked the button, the text only change one time after the loop is finished.

How can see changing text in every loop?










share|improve this question




























    up vote
    3
    down vote

    favorite












    This is UWP C# application. I want to display the i variable.



        private void btn2_Click(object sender, RoutedEventArgs e)
    {
    for(int i = 0; i<=10000000; i++)
    {
    textBlock2.Text = i.ToString();
    }
    }


    But after I clicked the button, the text only change one time after the loop is finished.

    How can see changing text in every loop?










    share|improve this question


























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      This is UWP C# application. I want to display the i variable.



          private void btn2_Click(object sender, RoutedEventArgs e)
      {
      for(int i = 0; i<=10000000; i++)
      {
      textBlock2.Text = i.ToString();
      }
      }


      But after I clicked the button, the text only change one time after the loop is finished.

      How can see changing text in every loop?










      share|improve this question















      This is UWP C# application. I want to display the i variable.



          private void btn2_Click(object sender, RoutedEventArgs e)
      {
      for(int i = 0; i<=10000000; i++)
      {
      textBlock2.Text = i.ToString();
      }
      }


      But after I clicked the button, the text only change one time after the loop is finished.

      How can see changing text in every loop?







      c# uwp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 5:17









      John

      10.5k31734




      10.5k31734










      asked Nov 8 at 5:15









      peachy__kat

      265




      265
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this



          private async void btn2_Click(object sender, RoutedEventArgs e)
          {
          for(int i = 0; i<=10000000; i++)
          {
          textBlock2.Text = i.ToString();
          await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
          }
          }


          When it executes to this line await Task.Delay(200);, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.






          share|improve this answer



















          • 1




            Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
            – Barr J
            Nov 8 at 5:23








          • 2




            BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
            – kennyzx
            Nov 8 at 5:25










          • 10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
            – vasily.sib
            Nov 8 at 6:05










          • @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
            – taffer
            Nov 8 at 6:36










          • I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
            – kennyzx
            Nov 8 at 6:43













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201919%2fhow-to-change-textblock-text-every-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this



          private async void btn2_Click(object sender, RoutedEventArgs e)
          {
          for(int i = 0; i<=10000000; i++)
          {
          textBlock2.Text = i.ToString();
          await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
          }
          }


          When it executes to this line await Task.Delay(200);, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.






          share|improve this answer



















          • 1




            Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
            – Barr J
            Nov 8 at 5:23








          • 2




            BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
            – kennyzx
            Nov 8 at 5:25










          • 10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
            – vasily.sib
            Nov 8 at 6:05










          • @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
            – taffer
            Nov 8 at 6:36










          • I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
            – kennyzx
            Nov 8 at 6:43

















          up vote
          3
          down vote



          accepted










          Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this



          private async void btn2_Click(object sender, RoutedEventArgs e)
          {
          for(int i = 0; i<=10000000; i++)
          {
          textBlock2.Text = i.ToString();
          await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
          }
          }


          When it executes to this line await Task.Delay(200);, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.






          share|improve this answer



















          • 1




            Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
            – Barr J
            Nov 8 at 5:23








          • 2




            BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
            – kennyzx
            Nov 8 at 5:25










          • 10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
            – vasily.sib
            Nov 8 at 6:05










          • @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
            – taffer
            Nov 8 at 6:36










          • I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
            – kennyzx
            Nov 8 at 6:43















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this



          private async void btn2_Click(object sender, RoutedEventArgs e)
          {
          for(int i = 0; i<=10000000; i++)
          {
          textBlock2.Text = i.ToString();
          await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
          }
          }


          When it executes to this line await Task.Delay(200);, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.






          share|improve this answer














          Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this



          private async void btn2_Click(object sender, RoutedEventArgs e)
          {
          for(int i = 0; i<=10000000; i++)
          {
          textBlock2.Text = i.ToString();
          await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
          }
          }


          When it executes to this line await Task.Delay(200);, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 5:36

























          answered Nov 8 at 5:20









          kennyzx

          9,66542263




          9,66542263








          • 1




            Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
            – Barr J
            Nov 8 at 5:23








          • 2




            BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
            – kennyzx
            Nov 8 at 5:25










          • 10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
            – vasily.sib
            Nov 8 at 6:05










          • @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
            – taffer
            Nov 8 at 6:36










          • I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
            – kennyzx
            Nov 8 at 6:43
















          • 1




            Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
            – Barr J
            Nov 8 at 5:23








          • 2




            BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
            – kennyzx
            Nov 8 at 5:25










          • 10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
            – vasily.sib
            Nov 8 at 6:05










          • @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
            – taffer
            Nov 8 at 6:36










          • I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
            – kennyzx
            Nov 8 at 6:43










          1




          1




          Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
          – Barr J
          Nov 8 at 5:23






          Better to use BackgroundWorker here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
          – Barr J
          Nov 8 at 5:23






          2




          2




          BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
          – kennyzx
          Nov 8 at 5:25




          BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
          – kennyzx
          Nov 8 at 5:25












          10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
          – vasily.sib
          Nov 8 at 6:05




          10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
          – vasily.sib
          Nov 8 at 6:05












          @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
          – taffer
          Nov 8 at 6:36




          @kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
          – taffer
          Nov 8 at 6:36












          I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
          – kennyzx
          Nov 8 at 6:43






          I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
          – kennyzx
          Nov 8 at 6:43




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201919%2fhow-to-change-textblock-text-every-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()