Created
March 8, 2017 21:04
-
-
Save cgddrd/288288e8dc4c689e06a2799ea048a052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
class Poller | |
{ | |
private Timer _timer; | |
public event EventHandler Tick; | |
public event Func<object, EventArgs, Task> OnPollEvent; | |
public Poller() | |
{ | |
_timer = new Timer(TimerTick, null, 3000, Timeout.Infinite); | |
} | |
private async void TimerTick(object state) | |
{ | |
if (OnPollEvent != null) | |
{ | |
await OnPollEvent.Invoke(this, EventArgs.Empty); | |
Console.WriteLine("Poll wait (3 secs)"); | |
} | |
_timer.Change(3000, Timeout.Infinite); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var timer = new Poller(); | |
timer.OnPollEvent += Update; | |
Console.ReadKey(); | |
} | |
private async static Task Update(object sender, EventArgs args) | |
{ | |
Console.WriteLine("Waiting for 10 secs."); | |
await Task.Delay(10000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment