bi01081993 / ncron

Automatically exported from code.google.com/p/ncron
Apache License 2.0
0 stars 0 forks source link

The process was terminated due to an unhandled exception. Exception Info: System.ArgumentOutOfRangeException Stack: at System.Threading.Timer.Change(Int64, Int64) at NCron.Service.SchedulingService.TimerCallbackHandler #18

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Start a Service
2. After some time, in the event viewer an exception will pop and the service 
stops abnormally.
3

What version of the product are you using? On what operating system?
Version 2.0. Windows 2008 Server. IIS 7.0. Framework 4

Please provide any additional information below.
I have changed the file NCron/ Service/ SchedulingService.cs:

private void TimerCallbackHandler(object data)
        {
            if (DateTime.Now >= _head.NextOccurence)
            {
                ThreadPool.QueueUserWorkItem(WaitCallbackHandler, _head);
                _head.Advance();
                _queue.Add(_head);
                _head = _queue.DeleteMin();
                TimerCallbackHandler(null);
            }
            else
            {
                var timeToNext = _head.NextOccurence - DateTime.Now;
                _timer.Change((long) timeToNext.TotalMilliseconds, Timeout.Infinite);
            }
        }

To:
private void TimerCallbackHandler(object data)
        {
            if (DateTime.Now >= _head.NextOccurence)
            {
                ThreadPool.QueueUserWorkItem(WaitCallbackHandler, _head);
                _head.Advance();
                _queue.Add(_head);
                _head = _queue.DeleteMin();
                TimerCallbackHandler(null);
            }
            else
            {
                var timeToNext = _head.NextOccurence - DateTime.Now;
                if (timeToNext.TotalMilliseconds > 0)
                    _timer.Change((long)timeToNext.TotalMilliseconds, Timeout.Infinite);
                else
                    _timer.Change((long)0, Timeout.Infinite);

            }
        }

Now everything is ok.
Thanks for Ncron.

Original issue reported on code.google.com by marcosou...@gmail.com on 19 Jan 2011 at 1:49

GoogleCodeExporter commented 8 years ago
Issue 19 has been merged into this issue.

Original comment by jsr%mala...@gtempaccount.com on 14 Feb 2011 at 8:56

GoogleCodeExporter commented 8 years ago
This issue is now fixed. I took a slightly different approach, merging the two 
comparisons of DateTime.Now and _head.NextOccurence into one:

        private void TimerCallbackHandler(object data)
        {
            var waitTime = _head.NextOccurence - DateTime.Now;
            var waitMilliseconds = (long) waitTime.TotalMilliseconds;

            if (waitMilliseconds > 0)
            {
                _timer.Change(waitMilliseconds, Timeout.Infinite);
                return;
            }

            ThreadPool.QueueUserWorkItem(WaitCallbackHandler, _head);
            _head.Advance();
            _queue.Add(_head);
            _head = _queue.DeleteMin();
            TimerCallbackHandler(null);
        }

A new build is on it's way up. Let me know if it causes any trouble.

Original comment by jsr%mala...@gtempaccount.com on 14 Feb 2011 at 9:05