-
-
Save parmarnishant/5509156 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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