Skip to content

Commit 0514a9d

Browse files
committed
Added disposable pattern to Timer implementation
1 parent 67a948c commit 0514a9d

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/MADE.Threading/Timer.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MADE.Threading
99
/// <summary>
1010
/// Defines a timer to use for performing actions on a tick.
1111
/// </summary>
12-
public class Timer : ITimer
12+
public class Timer : ITimer, IDisposable
1313
{
1414
private System.Threading.Timer timer;
1515

@@ -117,12 +117,31 @@ public void Stop()
117117
this.IsRunning = false;
118118
}
119119

120+
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
121+
public void Dispose()
122+
{
123+
this.Dispose(true);
124+
GC.SuppressFinalize(this);
125+
}
126+
120127
/// <summary>
121128
/// Invokes the <see cref="Tick"/> event, if attached.
122129
/// </summary>
123130
protected virtual void InvokeTick()
124131
{
125132
this.Tick?.Invoke(this, EventArgs.Empty);
126133
}
134+
135+
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
136+
/// <param name="disposing">
137+
/// A value indicating whether the object is being disposed by the <see cref="IDisposable.Dispose"/> method.
138+
/// </param>
139+
protected virtual void Dispose(bool disposing)
140+
{
141+
if (disposing)
142+
{
143+
this.timer?.Dispose();
144+
}
145+
}
127146
}
128147
}

0 commit comments

Comments
 (0)