Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>im just not sure how multiple timers running under a service would interact and how the threading will work when they all need to write to the same xml file.</p> <p>any thoughts on this?</p> </blockquote> <p>It won't work well.</p> <p>Timers are queued to the thread pool. The thread pool will only create a certain number of active threads at a time:</p> <p><a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/0ka9477y.aspx</a></p> <blockquote> <p>The number of operations that can be queued to the thread pool is limited only by available memory; however, the thread pool limits the number of threads that can be active in the process simultaneously</p> </blockquote> <p>In addition, writing to the same file from multiple threads is going to give you bad results.</p> <p>Not to mention it will be much harder to figure out what your application is doing if you have all those threads running.</p> <p><strong>A simpler approach</strong></p> <p>Take an existing timer and build your own timing system upon it that doesn't use threads.</p> <ul> <li>Create a structure defining a countdown entry.</li> </ul> <p>Code:</p> <pre><code>public class Countdown { public DateTime Time { get; set; } public event Action Elapsed { get; set; } public void RaiseElasped() { if(Elapsed != null) Elapsed(); } } </code></pre> <ul> <li><p>Read your file, and create: <code>Dictionary&lt;string, Countdown&gt;</code> (name -> countdown). Put the same code in each action that you were going to put in the timers.</p></li> <li><p>Create a <code>FileSystemWatcher</code> to look for changes to your input file. When the file is triggered, modify the dictionary as necessary.</p></li> <li><p>Create a single timer with a resolution of one minute.</p></li> <li><p>Every time that timer is triggered, check each item in the dictionary. If the target time has passed, call <code>RaiseElapsed</code>. (possibly remove it from the dictionary, too?)</p></li> </ul> <p>From this point, it is hard to determine exactly what you'd want to do, because you didn't describe what gets written to a file, when it happens, how often, or what happens once your countdown has elapsed. You should be able to figure out the rest yourself, though.</p> <p>The advantage of this approach is that single-threaded programs are much easier to debug, and you won't get any of the issues your question asked about (concurrent file access, or having too many threads running).</p> <p>The disadvantage is that it might bog down when you have a lot of entries. But if you have a lot of entries, you should really be using a database instead of of files.</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