Skip to content

Instantly share code, notes, and snippets.

@cgddrd
Created March 8, 2017 21:04
Show Gist options
  • Save cgddrd/288288e8dc4c689e06a2799ea048a052 to your computer and use it in GitHub Desktop.
Save cgddrd/288288e8dc4c689e06a2799ea048a052 to your computer and use it in GitHub Desktop.
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);
}
}
}
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