- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 431字
- 2021-07-03 00:11:56
How to do it...
- Start by adding the following variables to your form.
double timerTtl = 10.0D;
private DateTime timeToLive;
private int cacheValue;
- 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)";
}
- 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 = $"Timer TTL {timerTtl} sec (Running)";
}
- Now create a method that simulates some sort of longer running task. Delay this for a second. Use the Random keyword to generate a random number and assign it to the cacheValue variable. Set the time to live, start the timer, and return the cached value to the calling code.
public async Task<int> GetValue()
{
await Task.Delay(1000);
Random r = new Random();
cacheValue = r.Next();
timeToLive = DateTime.Now.AddSeconds(timerTtl);
timer1.Start();
return cacheValue;
}
- In the calling code, check to see if the time to live is still valid for the current cached value. If the time to live has expired, run the code that allocates and returns a Task<T> to get and set the cached value. If the time to live is still valid, just return the cached integer value.
You will notice that I am passing a Boolean out variable to indicate that a cached value has been read or set.
public ValueTask<int> LoadReadCache(out bool blnCached)
{
if (timeToLive < DateTime.Now)
{
blnCached = false;
return new ValueTask<int>(GetValue());
}
else
{
blnCached = true;
return new ValueTask<int>(cacheValue);
}
}
- The code for the button click uses the out variable isCachedValue and sets the text in the textbox accordingly.
private async void btnTestAsync_Click(object sender, EventArgs e)
{
int iVal = await LoadReadCache(out bool isCachedValue);
if (isCachedValue)
txtOutput.Text = $"Cached value {iVal} read";
else
txtOutput.Text = $"New value {iVal} read";
}
- When you finish adding all the code, run your application and click on the Test async button. This will read a new value from the GetValue() method, cache it, and start the time to live count down.

- If you click on the button again before the time to live has expired, the cached value is returned.

- When the time to live expires, clicking on the Test async button will call the GetValue() method again and the process repeats.

推薦閱讀
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- HBase從入門到實戰
- 數據結構(C語言)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Flowable流程引擎實戰
- Access 2010數據庫應用技術實驗指導與習題選解(第2版)
- Python程序設計開發寶典
- QPanda量子計算編程
- SQL Server 2008中文版項目教程(第3版)
- Deep Learning for Natural Language Processing
- Python Automation Cookbook
- 趣學數據結構
- Developing RESTful Web Services with Jersey 2.0
- 深入理解Android:WebKit卷