In general, such a problem, tied the beginning of the cycle to a button in the usual way.

for (i = 0; i <= 50; ++i) { Thread.Sleep(100); label.Content = i.ToString(); } 

But the label is updated only when the cycle reaches the final number - 50, how to make 1, 2, 3, 4 successively rewritten to the label ... and so on. The progress bar should also be filled.

But if I deal with the label, then the progress bar can also be finalized.

1 answer 1

If you do a button click handler, I advise you to use async / await:

 private async void Button_Click(object sender, RoutedEventArgs e) { Button.IsEnabled = false; for (var i = 0; i <= 50; ++i) { await Task.Delay(100); label.Content = i.ToString(); } Button.IsEnabled = true; } 

I also recommend using the MVVM approach. For your task you will need to deal with data binding and asynchronous commands. A good example of the implementation of asynchronous commands can be found here .

  • I wanted to implement through backgroundworker - test19
  • 2
    @SOFL backgroundworker => outdated technology - Bulson 5:56
  • Then you can clarify, that is, backgroundworker / async / await is essentially the same thing, only async / await are more technologically new and desirable to use? - test19
  • one
    @SOFL, this is not exactly the same thing, although they serve the same purpose - to simplify the programmer’s life when writing asynchronous code. BackgroundWorker and async / await offer different approaches to organizing and writing asynchronous code. More details about the patterns of asynchronous operations in .NetFramework can be found here . And yes, BackgroundWorker is considered obsolete. - Vlad