Reputation: 213
I have the folowing code,
now the question is what is the best way to preform this:
also take in notice the "minAdd" can pass 60, meaning 90min add (an hour an half etc)
thanks,
int minAdd = Convert.ToInt16(txtMinAdd.text);
DateTime now = DateTime.Now;
DateTime nextEvent = DateTime.Now.AddMinutes(minAdd);
TimeSpan diff = now - nextEvent;
if (diff > minAdd) -------------- PROBLEM HERE
{
//act here
}
Upvotes: 0
Views: 162
Reputation: 1499770
EDIT: As noted by Reed, the code you've shown is pretty pointless. I assume you actually want to get nextEvent
from somewhere else.
I suspect you just want:
if (diff.TotalMinutes > minAdd)
{
}
Or you could use:
TimeSpan minTimeSpan = TimeSpan.FromMinutes(Convert.ToInt16(txtMinAdd.text));
...
if (diff > minTimeSpan)
{
}
Upvotes: 4
Reputation: 564323
Since diff
is based on nextEvent
, which is based exactly on minAdd
, there is no reason for this check - it will never be true.
Also, in your code, diff
will always be negative if minAdd
is positive, as you're subtracting a future event (nextEvent
) from DateTime.Now
.
If you are trying to schedule an event to occur at a point in time, you may want to consider using a Timer
, and scheduling the timer to occur at some point in time based on the event time:
DateTime nextEvent = DateTime.Now.AddMinutes(minAdd);
TimeSpan diff = nextEvent - DateTime.Now;
// Schedule a timer to occur here
someTimer.Interval = diff.TotalMilliseconds; // Timer intervals are typically in ms
Upvotes: 1