Skip to content

Commit

Permalink
chore: use method signature overloads instead of dynamic in scheduler (
Browse files Browse the repository at this point in the history
  • Loading branch information
daveleek authored Sep 3, 2024
1 parent 2be6b9b commit bbfbd0f
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/Unleash/Internal/UnleashExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ internal static string ConvertToString(this Stream stream)
}
}

internal static void SafeTimerChange(this Timer timer, dynamic dueTime, dynamic period, ref bool disposeEnded)
internal static void SafeTimerChange(this Timer timer, int dueTime, int period, ref bool disposeEnded)
{
if (dueTime.GetType() != period.GetType())
throw new Exception("Data types has to match. (Int32 or TimeSpan)");

if (!(dueTime.GetType() != typeof(int) || dueTime.GetType() != typeof(TimeSpan)))
throw new Exception("Only System.Int32 or System.TimeSpan");
try
{
timer?.Change(dueTime, period);
}
catch (ObjectDisposedException)
{
// race condition with Dispose can cause trigger to be called when underlying
// timer is being disposed - and a change will fail in this case.
// see
// https://msdn.microsoft.com/en-us/library/b97tkt95(v=vs.110).aspx#Anchor_2
if (disposeEnded)
{
// we still want to throw the exception in case someone really tries
// to change the timer after disposal has finished
// of course there's a slight race condition here where we might not
// throw even though disposal is already done.
// since the offending code would most likely already be "failing"
// unreliably i personally can live with increasing the
// "unreliable failure" time-window slightly
throw;
}
}
}

internal static void SafeTimerChange(this Timer timer, TimeSpan dueTime, TimeSpan period, ref bool disposeEnded)
{
try
{
timer?.Change(dueTime, period);
Expand Down

0 comments on commit bbfbd0f

Please sign in to comment.