C# loop oneshot function

I have an interesting situation here. Language is C#, using Visual Studio.

I have a function-loop that is going to keep running through.

When a certain event triggers, I want a function to be called using an IF statement.

Is there a way I can have this function not be called again under a certain time delay?

For instance, Function A is called....Function A cannot be called no matter what in the next 20 seconds again.

After 20 seconds, Function A can be called again if the IF statement exists.

I have a function that is texting my phone, currently my loop is super fast so it texts my phone like 20 times in a few seconds. I want to limit this. I also cannot have the event trigger a while loop to make it go through another loop.

I need the loop to keep running its cycle but skip the function that texts my phone until a certain time has passed.

Use Timer:

Timer myTimer = new Timer();
 myTimer.Interval = 200; //double timer interval in ms
 myTimer.Elapsed += myTimer_Elapsed;
 myTimer.Start();

Then put your function inside myTimer_Elapsed:

void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        your_function();
    }

Stop it using myTimer.Stop();

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image