DateTime.Now as c # parameter value
I have a method that has a parameter of type DateTime that shouldn't be in the past, which indicates when some other stuff is done. So naturally I want to use this method and pass DateTime.Now to make things happen as soon as possible.
The problem is that DateTime.Now at the time of the call and DateTime.Now at the time it is actually checked will probably be different values, since I can make sure the date is not in the past, but allow the method to be called with what something like DateTime.Now? I don't want to add magic numbers here, but a general solution.
UPDATE. I found this question How often is DateTime.Now updated
source to share
It seems to me that you have two options:
- Add to that a suitable small amount of time (say 1 second) so that you start broadcasting almost immediately. It will work, but it's not nice.
-
Add another method (overload) that does not take a time parameter, but instead takes action immediately, for example
DoAction (attime); - will perform the action at the specified time
DoAction (); - will perform the action now
The latter is a nicer solution, but obviously there can be problems with how / where the method is available to use (and assumes that you can add the method anyway).
source to share
If you want things to happen as soon as possible, why not rewrite your method to run right after you call it, or create an overload without a DateTime parameter to run right after you call it.
Using the overload will of course allow you to run your code at once in the future (using the DateTime overload) or all at once.
source to share
If you are checking that the passed DateTime parameter is equal to or greater than DateTime.Now at the beginning of the function, it is highly unlikely that the system time would change between the time the function was called with DateTime.now and when the check was made. The system time is only updated every 10 or 15 milliseconds, and the actual call time of the function is much less.
To be sure that this never happens, you will need to check the past tense for DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 0, 25))
, which would slightly ease the tense that was taken in the past.
source to share