forked from microsoft/coyote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerSample.cs
70 lines (61 loc) · 2.24 KB
/
TimerSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Coyote;
using Microsoft.Coyote.Actors;
using Microsoft.Coyote.Actors.Timers;
using Microsoft.Coyote.Samples.Common;
namespace Coyote.Examples.Timers
{
[OnEventDoAction(typeof(TimerElapsedEvent), nameof(HandleTimeout))]
[OnEventDoAction(typeof(CustomTimerEvent), nameof(HandlePeriodicTimeout))]
internal class TimerSample : Actor
{
/// <summary>
/// Timer used in a periodic timer.
/// </summary>
private TimerInfo PeriodicTimer;
/// <summary>
/// The log to write output to
/// </summary>
private readonly LogWriter Log = LogWriter.Instance;
/// <summary>
/// A custom timer event
/// </summary>
internal class CustomTimerEvent : TimerElapsedEvent
{
/// <summary>
/// Count of timeout events processed.
/// </summary>
internal int Count;
}
protected override Task OnInitializeAsync(Event initialEvent)
{
this.Log.WriteWarning("<Client> Starting a non-periodic timer");
this.StartTimer(TimeSpan.FromSeconds(1));
return base.OnInitializeAsync(initialEvent);
}
private void HandleTimeout(Event e)
{
TimerElapsedEvent te = (TimerElapsedEvent)e;
this.Log.WriteWarning("<Client> Handling timeout from timer");
this.Log.WriteWarning("<Client> Starting a period timer");
this.PeriodicTimer = this.StartPeriodicTimer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), new CustomTimerEvent());
}
private void HandlePeriodicTimeout(Event e)
{
this.Log.WriteWarning("<Client> Handling timeout from periodic timer");
if (e is CustomTimerEvent ce)
{
ce.Count++;
if (ce.Count == 3)
{
this.Log.WriteWarning("<Client> Stopping the periodic timer");
this.Log.WriteWarning("<Client> Press ENTER to terminate.");
this.StopTimer(this.PeriodicTimer);
}
}
}
}
}