Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>Thread.Sleep</code> outside of test code is generally not ideal.</p> <p>You can use <code>System.Threading.Timer</code> objects for both these requirements.</p> <hr> <p><strong>EDIT</strong> if you want to prevent concurrent calls, you can use a lock to implement mutual exclusion.</p> <p>Create an object to lock on that can be seen by both timer handlers:</p> <pre><code>public object gate = new object(); </code></pre> <hr> <p>For the polling, you want to set up a timer like this:</p> <pre><code>var pollTimer = new Timer( HandlePoll, null, 0, Timeout.Infinite ); ... void HandlePoll( object state ) { lock ( gate ) { DoPoll(); } pollTimer.Change( pollInterval, Timeout.Infinite ); } </code></pre> <p>I've set the period to <code>Timeout.Infinte</code> so the timer doesn't repeat. This means <code>pollInterval</code> is the time between one poll finishing and another starting - which will be different to the poll period if <code>DoPoll()</code> takes some time.</p> <p>Doing it this way also prevents the timer ticking again while a previous poll is still running.</p> <hr> <p>You can use the same principal to send the commands, but you will have to manage the current index into the <code>sequencesList</code>:</p> <pre><code>var seqTimer = new Timer( HandleSequence, null, 0, Timeout.Infinate ); int seqIndex = 0; ... void HandleSequence( object state ) { var opcode = sequencesList[sequenceName][ seqIndex ]; byte register = opcode.register; bool bit = opcode.bit; int duration = opcode.duration; lock( gate ) { SetRegister(register, bit, false); } seqIndex++; if ( seqIndex &lt; sequencesList[sequenceName].Count ) { seqTimer.Change( duration, Timeout.Infinte ); } } </code></pre> <p>Or something along those lines ( but with suitable error handling! )</p> <p>You can handle termination and cancellation by just disposing the timers.</p> <hr> <p>BTW, there is an excellent ( and free ) ebook about threading that you could read:</p> <p><a href="http://www.albahari.com/threading/" rel="nofollow"><code>Albahari: Threading in C#</code></a></p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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