How to do it...

  1. Start by adding the following variables to your form.
        double timerTtl = 10.0D;        private DateTime timeToLive;        private int cacheValue;
  1. In the form load event, set the label with the timer text. 
Strictly speaking, this is all just fluff. It's not really necessary when it comes to illustrating generalized async return types, but it helps us to visualize and understand the concept.
        private void Form1_Load(object sender, EventArgs e)        {          lblTimer.Text = $"Timer TTL {timerTtl} sec (Stopped)";         }
  1. Set the timer interval on the designer to 1000 ms and add the following code to the timer1_Tick event.
        private void timer1_Tick(object sender, EventArgs e)        {          if (timerTtl == 0)          {            timerTtl = 5;          }          else          {            timerTtl -= 1;           } lblTimer.Text ...

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.