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

+2


source to share


9 replies


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).

+5


source


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.

+4


source


This is a philosophical issue - DateTime.Now is always in the past.

+3


source


Compare the parameter when entering DateTime.Now. If it's in the past, use DateTime.Now.

Alternatively, don't use the parameter; just use DateTime.Now.

+1


source


You will have to have some tolerance when testing if the time is close enough.

+1


source


Let your function take Nullable<DateTime>

. Interpret time zero to mean "do it now".

+1


source


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.

+1


source


Throwing ArgumentException if your arg date is in the past? This seems to be the easiest.

0


source


For the parameter, use a delegate that returns a value DateTime

. When you call a function, pass a delegate pointing to the function that returns DateTime.Now

.

0


source







All Articles