Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <a href="http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx" rel="noreferrer"><code>System.Threading.Timer</code></a>. You can specify a method to call periodically.</p> <p>Example:</p> <pre><code>Timer timer = new Timer(Callback, null, TimeSpan.Zero, TimeSpan.FromMinutes(5)); public void Callback(object state) { Console.WriteLine("The current time is {0}", DateTime.Now); } </code></pre> <p>You can use the second parameter to pass state to the callback.</p> <p>Note that you'll need to keep your application alive somehow (e.g., run it as a service).</p> <p>As for how to make sure that it runs at <code>hh:mm</code> where <code>mm % 5 == 0</code>, you can do the following.</p> <pre><code>DateTime now = DateTime.Now; int additionalMinutes = 5 - now.Minute % 5; if(additionalMinutes == 0) { additionalMinutes = 5; } var nearestOnFiveMinutes = new DateTime( now.Year, now.Month, now.Day, now.Hour, now.Minute, 0 ).AddMinutes(additionalMinutes); TimeSpan timeToStart = nearestOnFiveMinutes.Subtract(now); TimeSpan tolerance = TimeSpan.FromSeconds(1); if (timeToStart &lt; tolerance) { timeToStart = TimeSpan.Zero; } var Timer = new Timer(callback, null, timeToStart, TimeSpan.FromMinutes(5)); </code></pre> <p>Note that the <code>tolerance</code> is necessary in case this code is executing when <code>now</code> is very close to the nearest <code>hh:mm</code> with <code>mm % 5 == 0</code>. You can probably get away with a value smaller than one second but I'll leave that to you.</p>
 

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