Skip to content

Instantly share code, notes, and snippets.

@parmarnishant
Created May 3, 2013 13:38
Show Gist options
  • Select an option

  • Save parmarnishant/5509156 to your computer and use it in GitHub Desktop.

Select an option

Save parmarnishant/5509156 to your computer and use it in GitHub Desktop.
Observable.Timer(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10))
.ObserveOn(Scheduler.ThreadPool.AsSafe())
.Subscribe(_=> ThreadMethod());
public static class SafeSchedulerExtensions
{
public static IScheduler AsSafe(this IScheduler scheduler)
{
return new SafeScheduler(scheduler);
}
private class SafeScheduler : IScheduler
{
private IScheduler _source;
public SafeScheduler(IScheduler scheduler) {
this._source = scheduler;
}
public DateTimeOffset Now { get { return _source.Now; } }
public IDisposable Schedule(Action action, TimeSpan dueTime)
{
return _source.Schedule(Wrap(action), dueTime);
}
public IDisposable Schedule(Action action)
{
return _source.Schedule(Wrap(action));
}
private Action Wrap(Action action)
{
return () => {
try {
action();
}
catch (Exception e) {
// Log and report the exception.
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment