1

I have a for loop where I calculate the value of something and display it, for example

void Button_Click(object sender, RoutedEventArgs e)
{
    InitializeComponent();

    Random rand = new Random();
    for (i = 0; i <= 30; i++)        
    {
       rnd = rand.Next(1,10);
       value += i + rnd;
       display.Content = value;
    }
}

The problem is, I want to see the value overwritten after each cycle, delayed by X seconds, how could I achieve that in WPF?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Poklonc
  • 13
  • 2
  • Take a look at the [DispatcherTimer](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx) class. – Dan Bryant Sep 02 '14 at 13:57
  • Just delay? A sleep in a thread pool call (or Task) then dispatch to main thread for update. Repetitive? A dispatchedtimer. – Adriano Repetti Sep 02 '14 at 13:57
  • I tried Thread.Sleep(), but it stops the whole thread and then completes the whole loop. Task.Delay() works wonders, thank you – Poklonc Sep 02 '14 at 14:35

1 Answers1

5

Use Task.Delay to execute some code after a period of time.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    InitializeComponent();

    Random rand = new Random();
    for (i = 0; i <= 30; i++)        
    {
       rnd = rand.Next(1,10);
       value += i + rnd;
       display.Content = value;
       await Task.Delay(Timespan.FromSeconds(1));
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • I have a warning flag in my mind about returning `void` from `async` methods. – Jodrell Sep 02 '14 at 14:02
  • 1
    @Jodrell, this was the intended use case for async void (asynchronous event handlers), but yes, it would be a good idea for the OP to eventually split off an async method returning Task from this event handler. This is still a good simple solution in the interim while the OP is learning. – Dan Bryant Sep 02 '14 at 14:04
  • 1
    @Jodrell This is the event handler for an event. This exact situation is literally exactly why they made `async void` legal in the first place. – Servy Sep 02 '14 at 14:04
  • After research I agree http://stackoverflow.com/questions/19415646/should-i-avoid-async-void-event-handlers – Jodrell Sep 02 '14 at 14:04
  • @DanBryant What would that change gain him? – Servy Sep 02 '14 at 14:05
  • Thank You for the quick answer, I'm learning c# (and programming in general), sorry for the stupid question. I've googled for about 2 hours and found only complicated and not working solutions :S This one works like a charm – Poklonc Sep 02 '14 at 14:38
  • 1
    @Servy, As mentioned in the answer to the question Jodrell linked, the main benefit is to isolate the logic from the event handler (most likely in a View Model if this is eventually moved to an MVVM pattern), which allows for unit testing. The Task instance can also be used to explicitly handle Exceptions at some specific later point where it makes sense to do so. Not pressing concerns for the OP at this point, but worth considering in the future. – Dan Bryant Sep 02 '14 at 15:35
  • @DanBryant The method is already a top level method. Exception handling would either be done in this method, or by handing any exceptions thrown at an application level. – Servy Sep 02 '14 at 15:37