Skip to content

Commit 42e11a1

Browse files
committed
Allow extending ScheduledAction.
1 parent 529887f commit 42e11a1

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

Core/Threading/ScheduledAction.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public ScheduledAction(Action action)
5555
/// <returns>True if action has been cancelled.</returns>
5656
public bool Cancel()
5757
{
58-
if (this.token == null)
58+
if (this.token is null)
5959
return false;
6060
lock (this)
6161
{
62-
if (this.token != null)
62+
if (this.token is not null)
6363
{
64-
this.SynchronizationContext.CancelDelayed(this.token);
64+
this.CancelAction(this.token);
6565
this.token = null;
6666
return true;
6767
}
@@ -70,6 +70,15 @@ public bool Cancel()
7070
}
7171

7272

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+
7382
/// <summary>
7483
/// Execute action on current thread immediately. The scheduled execution will be cancelled.
7584
/// </summary>
@@ -117,7 +126,18 @@ public bool ExecuteIfScheduled()
117126
/// <summary>
118127
/// Check whether execution has been scheduled or not.
119128
/// </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);
121141

122142

123143
/// <summary>
@@ -127,10 +147,10 @@ public bool ExecuteIfScheduled()
127147
[MethodImpl(MethodImplOptions.Synchronized)]
128148
public void Reschedule(int delayMillis = 0)
129149
{
130-
if (this.token != null)
131-
this.SynchronizationContext.CancelDelayed(this.token);
150+
if (this.token is not null)
151+
this.CancelAction(this.token);
132152
object? token = null;
133-
token = this.SynchronizationContext.PostDelayed(_ =>
153+
token = this.PostAction(_ =>
134154
{
135155
lock (this) // barrier to make sure that variable 'token' has been assigned
136156
{ }
@@ -163,10 +183,10 @@ public void Reschedule(TimeSpan delay)
163183
[MethodImpl(MethodImplOptions.Synchronized)]
164184
public void Schedule(int delayMillis = 0)
165185
{
166-
if (this.token != null)
186+
if (this.token is not null)
167187
return;
168188
object? token = null;
169-
token = this.SynchronizationContext.PostDelayed(_ =>
189+
token = this.PostAction(_ =>
170190
{
171191
lock (this) // barrier to make sure that variable 'token' has been assigned
172192
{ }

0 commit comments

Comments
 (0)