Bug 44669 - System.Threading.Timer behavior different compared to Windows
Summary: System.Threading.Timer behavior different compared to Windows
Status: RESOLVED ANSWERED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.4.2 (C7SR1)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-09-23 07:54 UTC by Menno
Modified: 2016-09-23 10:01 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Menno 2016-09-23 07:54:49 UTC
This excerpt from my code produces different behavior on Mono compared to Windows.

On Windows, the timer is re-initialized each time the OnHorizontalAxisChanged event is fired. Only after the event has not fired for 250 ms is the TimerElapsed called. This is what I want.

On Mono (version 4.4.2.11/f72fe45) I get a lot of TimerElapsed calls, even if I re-initialize the timer like in the code below. I have tried various other approaches, but they all lead to TimerElapsed being called when this is clearly not wanted.

    private Timer _hTimer;
    protected virtual void OnHorizontalAxisChanged(object sender, AxisChangedEventArgs args)
    {
        if (null != _hTimer)
        {
            _hTimer.Dispose();
            _hTimer = new Timer(OnTimerElapsed, null, TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);

            LogTo.Debug("Timer re-initialized.");
            return;
        }

        _hTimer = new Timer(OnTimerElapsed, null, TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
    }

    private void OnTimerElapsed(object state)
    {
        LogTo.Debug("Timer elapsed.");
        
        _hTimer?.Dispose();
        _hTimer = null;
    }
Comment 1 Menno 2016-09-23 10:01:19 UTC
My apologies. The framerate on Linux (using a remote desktop NX) was so low that the time-out would be triggered each frame. With a larger TimeSpan/Interval the problem has been resolved.