tags:

views:

124

answers:

4

I am trying pause time using C#/.NET. I am able to set the time but I would like to set the time to pause.

Does anyone know how to do this?

Command Prompt suggestions would also be helpful.

I'm using Visual Studio 2010 if that matters.

A: 

I think you are talking about stopping windows time, I imagine this is a not a feature of windows to do this. What are you actually tryting to achieve (other than break windows) there may be alternate solutions.

MikeG
@ MikeG -- I am writing tests at a specific time and It need's to stay the same as the tests run. It's boundary testing, I would assume since you can change time to the past there should be no problem pausing the windows timer.
chrissygormley
Mock out the clock into a replaceable fake class, which can return the same value throughout your tests.
Lasse V. Karlsen
A: 

Well, I'm not sure why you'd want to do this, but, since there is no such thing as a 'PauseTime' function in windows, why don't you just have a service that marks the current time, and then continously sets the system time to that start time. That should achieve the effect of keeping the system time the same.

Kazar
@ Kazar -- That is what I have done at the minute but I was hoping for a way to just freeze it. Thanks anyway
chrissygormley
+3  A: 

For testing? or real life?

For testing you can mock DateTime.Now (although it is easier to have an IClock interface). For real usage - I just wouldn't.

Or do you just mean Thread.Sleep?

Marc Gravell
@ Marc -- It's the system time I need to freeze, I need to check a report has been entered this week not next week, I check it at 23:59:59 before the next week. It's to check the system does not think it's the next week. This process takes longer than 1 sec to check the reports in the automated tests.
chrissygormley
You don't need to freeze system time to do this. Nor would that be reliable, because what if freezing system time takes more than a second? Either run the process earlier or just improve your time handling code. If it's less than an hour into the week when the code runs, you know you should be looking at the previous week. Just add that to your code.
Daniel Straight
@chrissygormley, As others have indicated, the best way to do this is to isolate all dependencies on time so that you can mock the timing behavior for your tests. This isn't always easy, especially if you have a legacy codebase. I personally like to have an ITimerService providing various timing-related functions (reporting DateTime.Now, callbacks after time, measuring elapsed time, etc.) that I can inject into my classes needing awareness of time.
Dan Bryant
A: 

If you need to find out elapsed time for some debugging work, there's the System.Diagnotics.Stopwatch class. The page on MSDN has some decent examples on how to use it. I don't know what type of precision you're looking for, so maybe this isn't the class to use if you're looking for high performance timing diagnostics.

Hope this helps!

David Hoerster