Note that there are some explanatory texts on larger screens.

plurals
  1. POTime based Method execution in C#
    primarykey
    data
    text
    <p>I have a method <code>send()</code> that I wish to execute/call every 1 second. I'm having difficulty implementing this. So far this is what I have come up with in my main program:</p> <pre><code>bool done = false; while (!done) { string vCurrent = RandomVoltage(220, 240) + "" + RandomCurrent(10, 13); int seconds = RandomSec(); if (isEven(seconds)) send(vCurrent, "169.254.156.135");//send the string to the ip address } </code></pre> <p>So basically I try call my <code>send()</code> method for every second of the current time that is even, and I skip the odd seconds, here is how I <em>tried</em> to implement that with my <code>RandomSec()</code> and <code>isEven()</code> methods:</p> <pre><code> private static readonly object syncLock = new object(); public static int RandomSec() { lock (syncLock) { return DateTime.Now.Second; } } public static bool isEven(int sec) { if ((sec % 2) == 0) return true; else return false; } </code></pre> <p>Now the problem is when I run the while loop in my program, my <code>send()</code> method sends a big bunch of strings in 1 second, then pauses for 1 second and then sends another big bunch of messages when the current second is even again. How can I get my program to execute my <code>send()</code> method only ONCE every 1 second, so that the <code>send()</code> method sends only 1 string every even second rather than say 20/30 of them. Is it possible for me to call my <code>send()</code> method in a time controlled loop? Any help is greatly appreciated.</p> <p>Thanks in advance.</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.
 

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