Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Im interpreting your questions as a request for an implementatation of the IdleNotifier interface specified below. Also you state that ActionOccured() needs to be fast. </p> <pre><code>public delegate void IdleCallback(); public interface IdleNotifier { // Called by threadpool when more than IdleTimeSpanBeforeCallback // has passed since last call on ActionOccured. IdleCallback Callback { set; } TimeSpan IdleTimeSpanBeforeCallback { set; } void ActionOccured(); } </code></pre> <p>I provide an implementation with System.Threading.Timer below. Important points about the implementation:</p> <ul> <li>We accept that the timer can wake up at any time and make sure this is ok.</li> <li>Since we assume the timer wakes relatively seldom we can do expensive work at these times.</li> <li>Since we can do all logic in the timer callback all we need to do to "push the timer" is to remeber when last we pushed it.</li> </ul> <p>Implementation:</p> <pre><code>public class IdleNotifierTimerImplementation : IdleNotifier { private readonly object SyncRoot = new object(); private readonly Timer m_Timer; private IdleCallback m_IdleCallback = null; private TimeSpan m_IdleTimeSpanBeforeEvent = TimeSpan.Zero; // Null means there has been no action since last idle notification. private DateTime? m_LastActionTime = null; public IdleNotifierTimerImplementation() { m_Timer = new Timer(OnTimer); } private void OnTimer(object unusedState) { lock (SyncRoot) { if (m_LastActionTime == null) { m_Timer.Change(m_IdleTimeSpanBeforeEvent, TimeSpan.Zero); return; } TimeSpan timeSinceLastUpdate = DateTime.UtcNow - m_LastActionTime.Value; if (timeSinceLastUpdate &gt; TimeSpan.Zero) { // We are no idle yet. m_Timer.Change(timeSinceLastUpdate, TimeSpan.Zero); return; } m_LastActionTime = null; m_Timer.Change(m_IdleTimeSpanBeforeEvent, TimeSpan.Zero); } if (m_IdleCallback != null) { m_IdleCallback(); } } // IdleNotifier implementation below public void ActionOccured() { lock (SyncRoot) { m_LastActionTime = DateTime.UtcNow; } } public IdleCallback Callback { set { lock (SyncRoot) { m_IdleCallback = value; } } } public TimeSpan IdleTimeSpanBeforeCallback { set { lock (SyncRoot) { m_IdleTimeSpanBeforeEvent = value; // Run OnTimer immediately m_Timer.Change(TimeSpan.Zero, TimeSpan.Zero); } } } } </code></pre> <p>There are many straight-forward performance improvements on this code.</p> <p>If anyone would be intrested in my first thoughts on this just ask me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload