|
4 | 4 | Public Class TimerRun
|
5 | 5 | Inherits Run
|
6 | 6 | Implements IDisposable
|
7 |
| - |
| 7 | + |
8 | 8 | Private _timer As Threading.DispatcherTimer = Nothing
|
9 |
| - |
| 9 | + Private ReadOnly Property _isTimerRunning |
| 10 | + Get |
| 11 | + Return _timer IsNot Nothing AndAlso _timer.IsEnabled |
| 12 | + End Get |
| 13 | + End Property |
| 14 | + |
10 | 15 | '定义依赖属性
|
11 |
| - Public Shared ReadOnly UpdateIntervalProperty As DependencyProperty = DependencyProperty.Register(NameOf(UpdateInterval), GetType(TimeSpan), GetType(TimerRun), New PropertyMetadata(TimeSpan.FromSeconds(1))) |
12 |
| - |
| 16 | + Public Shared ReadOnly UpdateIntervalProperty As DependencyProperty = |
| 17 | + DependencyProperty.Register( |
| 18 | + NameOf(UpdateInterval), |
| 19 | + GetType(TimeSpan), |
| 20 | + GetType(TimerRun), |
| 21 | + New PropertyMetadata(TimeSpan.FromSeconds(1))) |
| 22 | + '属性变化处理 |
| 23 | + Protected Overrides Sub OnPropertyChanged(e As DependencyPropertyChangedEventArgs) |
| 24 | + MyBase.OnPropertyChanged(e) |
| 25 | + If e.Property Is UpdateIntervalProperty AndAlso _timer IsNot Nothing Then |
| 26 | + _timer.Interval = UpdateInterval |
| 27 | + End If |
| 28 | + End Sub |
| 29 | + |
13 | 30 | 'UpdateInterval 属性
|
14 | 31 | Public Property UpdateInterval As TimeSpan
|
15 | 32 | Get
|
16 | 33 | Return CType(GetValue(UpdateIntervalProperty), TimeSpan)
|
17 | 34 | End Get
|
18 | 35 | Set
|
19 |
| - SetValue(UpdateIntervalProperty, value) |
| 36 | + If Value > TimeSpan.Zero Then |
| 37 | + SetValue(UpdateIntervalProperty, Value) |
| 38 | + End If |
20 | 39 | End Set
|
21 | 40 | End Property
|
22 |
| - |
| 41 | + |
23 | 42 | Public Property AutoStart As Boolean
|
24 |
| - |
25 |
| - Public Sub New(Optional autoStart As Boolean = False) |
| 43 | + |
| 44 | + Public Sub New(Optional interval As TimeSpan = Nothing, Optional autoStart As Boolean = False) |
| 45 | + _timer = New Threading.DispatcherTimer |
| 46 | + AddHandler _timer.Tick, AddressOf _timerTick |
| 47 | + Me.UpdateInterval = If(interval = Nothing, TimeSpan.FromSeconds(1), interval) |
26 | 48 | Me.AutoStart = autoStart
|
27 | 49 | End Sub
|
28 |
| - |
| 50 | + |
29 | 51 | '定时器事件
|
30 |
| - Public Event TimerTick As Action(Of TimerRun) |
31 |
| - |
32 |
| - Private Sub _TimerTick(sender As Object, e As EventArgs) |
| 52 | + Public Delegate Sub TimerTickDelegate(sender As TimerRun) |
| 53 | + Public Event TimerTick As TimerTickDelegate |
| 54 | + |
| 55 | + Private Sub _timerTick(sender As Object, e As EventArgs) |
33 | 56 | RaiseEvent TimerTick(Me)
|
34 | 57 | End Sub
|
35 |
| - |
| 58 | + |
36 | 59 | Private Sub OnLoaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
|
37 |
| - if AutoStart Then StartTimer() |
| 60 | + If AutoStart Then StartTimer() |
38 | 61 | End Sub
|
39 |
| - |
| 62 | + |
40 | 63 | Private Sub OnUnloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
|
41 | 64 | StopTimer()
|
42 | 65 | End Sub
|
43 |
| - |
| 66 | + |
44 | 67 | Public Sub StartTimer()
|
45 |
| - StopTimer() '确保没有重复的定时器 |
46 |
| - _timer = New Threading.DispatcherTimer With { .Interval = UpdateInterval } |
47 |
| - AddHandler _timer.Tick, AddressOf _TimerTick |
48 |
| - _timer.Start() |
49 |
| - End Sub |
50 |
| - |
51 |
| - Public Sub StopTimer() |
52 |
| - If _timer IsNot Nothing Then |
53 |
| - _timer.Stop() |
54 |
| - RemoveHandler _timer.Tick, AddressOf _TimerTick |
55 |
| - _timer = Nothing |
| 68 | + If Me.Dispatcher Is Nothing Then |
| 69 | + Log("[TimerRun] Dispatcher is null, unable to run", LogLevel.Critical) |
| 70 | + Return |
56 | 71 | End If
|
| 72 | + If Not _isTimerRunning Then _timer?.Start() |
57 | 73 | End Sub
|
58 |
| - |
59 |
| - '属性变化处理 |
60 |
| - Protected Overrides Sub OnPropertyChanged(e As DependencyPropertyChangedEventArgs) |
61 |
| - MyBase.OnPropertyChanged(e) |
62 |
| - If e.Property Is UpdateIntervalProperty AndAlso _timer IsNot Nothing Then |
63 |
| - _timer.Interval = UpdateInterval |
64 |
| - End If |
| 74 | + |
| 75 | + Public Sub StopTimer() |
| 76 | + If _isTimerRunning Then _timer?.Stop() |
65 | 77 | End Sub
|
66 |
| - |
67 |
| - '实现 IDisposable |
68 |
| - Private _disposed As Boolean = False |
| 78 | + |
| 79 | + Private _isDisposed = False |
69 | 80 | Public Sub Dispose() Implements IDisposable.Dispose
|
70 |
| - If Not _disposed Then |
71 |
| - StopTimer() |
72 |
| - _disposed = True |
73 |
| - End If |
74 |
| - GC.SuppressFinalize(Me) |
| 81 | + If _isDisposed Then Return |
| 82 | + _isDisposed = True |
| 83 | + '资源释放 |
| 84 | + RemoveHandler _timer.Tick, AddressOf _timerTick |
| 85 | + _timer?.Stop() |
| 86 | + _timer = Nothing |
75 | 87 | End Sub
|
76 | 88 |
|
77 | 89 | End Class
|
|
169 | 181 | lab.Inlines.Add(curRun)
|
170 | 182 |
|
171 | 183 | ' 用于存储需要随机化的文本段
|
172 |
| - Dim randomTextRuns As New List(Of TimerRun)() |
| 184 | + Dim randomTextRuns As New List(Of TimerRun) |
173 | 185 |
|
174 | 186 | For Each c As Char In text
|
175 | 187 | If c = "§" Then '下一字符是格式化代码
|
|
0 commit comments