System.Net.TimerThread.TimerQueue.CreateTimer C# (CSharp) Method

CreateTimer() private method

Creates new timers. This method is thread-safe.

private CreateTimer ( Callback callback, object context ) : Timer
callback Callback
context object
return System.Threading.Timer
            internal override Timer CreateTimer(Callback callback, object context)
            {
                TimerNode timer = new TimerNode(callback, context, Duration, _timers);

                // Add this on the tail.  (Actually, one before the tail - _timers is the sentinel tail.)
                bool needProd = false;
                lock (_timers)
                {
                    if (!(_timers.Prev.Next == _timers))
                    {
                        NetEventSource.Fail(this, $"Tail corruption.");
                    }

                    // If this is the first timer in the list, we need to create a queue handle and prod the timer thread.
                    if (_timers.Next == _timers)
                    {
                        if (_thisHandle == IntPtr.Zero)
                        {
                            _thisHandle = (IntPtr)GCHandle.Alloc(this);
                        }
                        needProd = true;
                    }

                    timer.Next = _timers;
                    timer.Prev = _timers.Prev;
                    _timers.Prev.Next = timer;
                    _timers.Prev = timer;
                }

                // If, after we add the new tail, there is a chance that the tail is the next
                // node to be processed, we need to wake up the timer thread.
                if (needProd)
                {
                    TimerThread.Prod();
                }

                return timer;
            }
TimerThread.TimerQueue