Last active
October 7, 2016 11:53
-
-
Save benhysell/9c00407d7b0550494e3e to your computer and use it in GitHub Desktop.
Rx Timer Example
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
Action work = () =>Console.Writeln("work!"); | |
Scheduler.Default.Schedule( | |
// start in so many seconds | |
TimeSpan.FromSeconds(60 - DateTime.Now.Second), | |
// then run every minute | |
() => Scheduler.Default.SchedulePeriodic(TimeSpan.FromMinutes(1), work)); |
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
//create a timer to trigger the event change in the future | |
var timer = Observable.Timer(DateTimeOffset.Now.AddMilliseconds(sendDelay.Value)); | |
timer.Subscribe(_ => | |
{ | |
var handler = RaiseMessageSendEvent; | |
if (null != handler) | |
handler(this, new MessageEventArguments(response)); | |
}); |
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
/// <summary> | |
/// Future change events | |
/// </summary> | |
CompositeDisposable futureSignalChangeEvents; | |
/// <summary> | |
/// clear out all old timers | |
/// </summary> | |
private void Reset() | |
{ | |
//clear out all old timers | |
futureSignalChangeEvents.Dispose(); | |
futureSignalChangeEvents = new CompositeDisposable(); | |
} | |
public void CreateFutureEvent() | |
{ | |
//create a timer to trigger the event change in the future | |
var timer = Observable.Interval(TimeSpan.FromMilliseconds(100)) | |
.Subscribe( | |
x => | |
{ | |
//do work here | |
}); | |
futureSignalChangeEvents.Add(futureEventSubscription); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment