Repository: dotnet/runtime
File: src/libraries/System.ComponentModel.TypeConverter/tests/TimerTests.cs (C#)

Code before:
            }
        }

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

                timer.AutoReset = false;
                timer.Elapsed += (sender, e) =>
                {
                    Interlocked.Increment(ref count);
                    mres.Set();
                };
                timer.Start();

                mres.Wait();
                Assert.False(timer.Enabled);
                Assert.Equal(1, count);

                count = 0;
                timer.AutoReset = true;
                Thread.Sleep(100);
                Assert.Equal(0, count);
                Assert.False(timer.Enabled);

                timer.Interval = 1;
                Thread.Sleep(100);
                Assert.Equal(0, count);
                Assert.False(timer.Enabled);
            }
        }

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

                timer.AutoReset = true;
                timer.Elapsed += (sender, e) =>
                {
                    if (Interlocked.Increment(ref count) >= 5)
                    {
                        mres.Set();
                    }

Review comment (performance):
The sleep duration of 100ms may be insufficient to reliably verify that the timer did not restart, especially on slower or heavily loaded systems. Consider increasing the sleep duration to at least 200-500ms or using a more deterministic approach with a longer wait combined with assertion on the count remaining zero.

This issue also appears in the following locations of the same file:
- line 146
- line 178

Code after:
            }
        }

        [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");
            }
        }
    }
}