Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public bool AutoReset
else if (_autoReset != value)
{
_autoReset = value;
if (_timer != null)
{
UpdateTimer();
}
UpdateTimer();
}
}
}
Expand Down Expand Up @@ -152,7 +149,11 @@ public bool Enabled

private void UpdateTimer()
{
Debug.Assert(_timer != null, $"{nameof(_timer)} was expected not to be null");
if (_timer is null || !_enabled)
{
return;
}

int i = (int)Math.Ceiling(_interval);
_timer.Change(i, _autoReset ? i : Timeout.Infinite);
}
Expand All @@ -172,10 +173,7 @@ public double Interval
}

_interval = value;
if (_timer != null)
{
UpdateTimer();
}
UpdateTimer();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void TestTimerStartAutoReset()
target = 10;
mres.Reset();
timer.AutoReset = true;
timer.Start();
mres.Wait();

timer.Stop();
Expand Down Expand Up @@ -114,5 +115,33 @@ public void TestInterval(double interval)
Assert.Equal(interval, timer.Interval);
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void SettingAutoResetOrIntervalOnDisabledOneShotTimerDoesNotRestartIt()
{
using (var timer = new TestTimer(1))
{
var mres = new ManualResetEventSlim();

timer.AutoReset = false;
timer.Elapsed += (sender, e) => mres.Set();
timer.Start();

mres.Wait();
Assert.False(timer.Enabled);

// Setting AutoReset should not restart a disabled timer
mres.Reset();
timer.AutoReset = true;
Assert.False(timer.Enabled);
Assert.False(mres.Wait(100), "Timer callback should not have fired after setting AutoReset");

// Setting Interval should not restart a disabled timer
mres.Reset();
timer.Interval = 1;
Assert.False(timer.Enabled);
Assert.False(mres.Wait(100), "Timer callback should not have fired after setting Interval");
}
}
}
}
Loading