@@ -55,13 +55,13 @@ public ScheduledAction(Action action)
55
55
/// <returns>True if action has been cancelled.</returns>
56
56
public bool Cancel ( )
57
57
{
58
- if ( this . token == null )
58
+ if ( this . token is null )
59
59
return false ;
60
60
lock ( this )
61
61
{
62
- if ( this . token != null )
62
+ if ( this . token is not null )
63
63
{
64
- this . SynchronizationContext . CancelDelayed ( this . token ) ;
64
+ this . CancelAction ( this . token ) ;
65
65
this . token = null ;
66
66
return true ;
67
67
}
@@ -70,6 +70,15 @@ public bool Cancel()
70
70
}
71
71
72
72
73
+ /// <summary>
74
+ /// Cancel posted action.
75
+ /// </summary>
76
+ /// <param name="token">Token returned from <see cref="PostAction"/> to identify the posted action.</param>
77
+ /// <returns>True if action has been cancelled successfully.</returns>
78
+ protected virtual bool CancelAction ( object token ) =>
79
+ this . SynchronizationContext . CancelDelayed ( token ) ;
80
+
81
+
73
82
/// <summary>
74
83
/// Execute action on current thread immediately. The scheduled execution will be cancelled.
75
84
/// </summary>
@@ -117,7 +126,18 @@ public bool ExecuteIfScheduled()
117
126
/// <summary>
118
127
/// Check whether execution has been scheduled or not.
119
128
/// </summary>
120
- public bool IsScheduled => this . token != null ;
129
+ public bool IsScheduled => this . token is not null ;
130
+
131
+
132
+ /// <summary>
133
+ /// Post action to underlying synchronization context.
134
+ /// </summary>
135
+ /// <param name="action">Action.</param>
136
+ /// <param name="state">State.</param>
137
+ /// <param name="delayMillis">Delay time in milliseconds.</param>
138
+ /// <returns>Token to identify the posted action.</returns>
139
+ protected virtual object PostAction ( SendOrPostCallback action , object ? state , int delayMillis ) =>
140
+ this . SynchronizationContext . PostDelayed ( action , state , delayMillis ) ;
121
141
122
142
123
143
/// <summary>
@@ -127,10 +147,10 @@ public bool ExecuteIfScheduled()
127
147
[ MethodImpl ( MethodImplOptions . Synchronized ) ]
128
148
public void Reschedule ( int delayMillis = 0 )
129
149
{
130
- if ( this . token != null )
131
- this . SynchronizationContext . CancelDelayed ( this . token ) ;
150
+ if ( this . token is not null )
151
+ this . CancelAction ( this . token ) ;
132
152
object ? token = null ;
133
- token = this . SynchronizationContext . PostDelayed ( _ =>
153
+ token = this . PostAction ( _ =>
134
154
{
135
155
lock ( this ) // barrier to make sure that variable 'token' has been assigned
136
156
{ }
@@ -163,10 +183,10 @@ public void Reschedule(TimeSpan delay)
163
183
[ MethodImpl ( MethodImplOptions . Synchronized ) ]
164
184
public void Schedule ( int delayMillis = 0 )
165
185
{
166
- if ( this . token != null )
186
+ if ( this . token is not null )
167
187
return ;
168
188
object ? token = null ;
169
- token = this . SynchronizationContext . PostDelayed ( _ =>
189
+ token = this . PostAction ( _ =>
170
190
{
171
191
lock ( this ) // barrier to make sure that variable 'token' has been assigned
172
192
{ }
0 commit comments