Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think constantly checking DateTime.Now against an end time that updates after the end time is reached seems computationally intensive, and I was wondering if there is more computationally efficient way to check a condition periodically.

What I have tried:

C#
DateTime CurrentTime = DateTime.Now;
DateTime EndTime = CurrentTime.AddMinutes(1);

while (TRUE)
{
CurrentTime = DateTime.Now;

if (CurrentTime>EndTime)
{
EndTime=CurrentTime.AddMinutes(1);
DOSOMETHING;
}
}
Posted
Updated 27-Sep-21 14:13pm
v3
Comments
BillWoodruff 28-Sep-21 12:40pm    
unless you terminate the while loop, it will never exit

Quote:
I think constantly checking DateTime.Now against an end time that updates after the end time is reached seems computationally intensive, and I was wondering if there is more computationally efficient way to check a condition periodically.

You should think about using a timer to set an event at 'end time'.
Timer in C#[^]
C# Language Tutorial - Timers[^]
 
Share this answer
 
Comments
Admin BTA 28-Sep-21 13:29pm    
Awesome, timers work like a charm. THANKS!
You would normally use a Timer and in the Timer Tick callback/event (depending on which Time you use,) you would check the current time against the scheduled time.
 
Share this answer
 
Comments
Admin BTA 28-Sep-21 13:29pm    
Awesome, timers work like a charm. THANKS!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900