Browse Subversion Repository
Contents of /ObservableTimer.cs
Parent Directory
| Revision Log
Revision 8 -
( show annotations)
( download)
Wed Feb 23 12:21:31 2011 UTC
(13 years, 3 months ago)
by aqua877
File size: 1700 byte(s)
バージョン情報ウィンドウへのロゴ追加
設定ウィンドウのイベントおよび初期化の実装すべて完了
コメント取得時のソーティングによるパフォーマンス低下を改善
| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Linq; |
| 4 |
|
| 5 |
namespace Aqua877.WinApp.IronLivetube |
| 6 |
{ |
| 7 |
public sealed class ObservableTimer : IObservable<long>, IDisposable |
| 8 |
{ |
| 9 |
public TimeSpan Interval |
| 10 |
{ |
| 11 |
get |
| 12 |
{ |
| 13 |
return this.WhenPeriodChanges.First(); |
| 14 |
} |
| 15 |
set |
| 16 |
{ |
| 17 |
this.WhenPeriodChanges.OnNext(value); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
private readonly BehaviorSubject<TimeSpan> WhenPeriodChanges; |
| 22 |
private readonly IConnectableObservable<long> Timer; |
| 23 |
private IDisposable Connection; |
| 24 |
|
| 25 |
public ObservableTimer(TimeSpan dueTime, TimeSpan period) |
| 26 |
{ |
| 27 |
this.WhenPeriodChanges = new BehaviorSubject<TimeSpan>(period); |
| 28 |
this.Timer = Start(dueTime, WhenPeriodChanges).Publish(); |
| 29 |
} |
| 30 |
|
| 31 |
private static IObservable<long> Start(TimeSpan dueTime, IObservable<TimeSpan> whenPeriodChanges) |
| 32 |
{ |
| 33 |
var nextPeriod = whenPeriodChanges.Skip(1); |
| 34 |
|
| 35 |
return (from period in whenPeriodChanges.Take(1) from time in Observable.Timer(dueTime, period) select time).TakeUntil(nextPeriod).Concat(from period in whenPeriodChanges from time in Observable.Interval(period).TakeUntil(nextPeriod) select time); |
| 36 |
} |
| 37 |
|
| 38 |
public void Start() |
| 39 |
{ |
| 40 |
if (this.Connection == null) |
| 41 |
{ |
| 42 |
lock (this.Timer) |
| 43 |
{ |
| 44 |
if (this.Connection == null) |
| 45 |
{ |
| 46 |
this.Connection = this.Timer.Connect(); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public void Stop() |
| 53 |
{ |
| 54 |
lock (this.Timer) |
| 55 |
{ |
| 56 |
if (this.Connection != null) |
| 57 |
{ |
| 58 |
this.Connection.Dispose(); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public IDisposable Subscribe(IObserver<long> observer) |
| 64 |
{ |
| 65 |
return this.Timer.ObserveOnDispatcher().Subscribe(observer); |
| 66 |
} |
| 67 |
|
| 68 |
void IDisposable.Dispose() |
| 69 |
{ |
| 70 |
this.Stop(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
|