c# timer without timer

public void OnStart()
{
    _stop.Reset();
    _registeredWait = ThreadPool.RegisterWaitForSingleObject(_stop, 
        new WaitOrTimerCallback(PeriodicProcess), null, 5000, false);
}

public void OnStop()
{
    _stop.Set();
}

private void PeriodicProcess(object state, bool timeout)
{
    if (timeout)
    {
        // Periodic processing here
    }
    else
        // Stop any more events coming along
        _registeredWait.Unregister(null);
}

private ManualResetEvent _stop = new ManualResetEvent(false);
private RegisteredWaitHandle _registeredWait;


//It's the method for periodically calling some methothds without using Timers. I think it is more elegant solution for this.

//Waiting process works in separate thread pool. Calling method works in main thread. Be carefull with that.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.