Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Chris Kopcow
Chris Kopcow
2,852 Points

Using Timers and DateTime in a C# Virtual Pet

Since finishing the Intermediate C# course, I've been working on a virtual pet, as was suggested, to practice using DateTime, Timers and interfaces.

As you can see from the snapshot of the Virtual Pet program (https://w.trhou.se/4jv0ey9i6c), I want my pet to 1) only be active between 1:30pm and 3:30pm and 2) have its Exercise, Food, and Fun ratings -- and therefore its wellBeing rating -- decrease every 30 seconds.

Unfortunately... 1) No matter what DateTime hour parameters I set, the program always asks me to "come back at 1:30!" even if I'm playing within the set hours and closes. 2) Despite reading a ton of documentation about Timers, I'm fairly certain I'm missing something in its implementation. (Because of issue #1, I haven't been able to test if the timer works and does what I want it to.) Is there something I'm doing wrong when setting up the Timer?

Any help would be greatly appreciated it!

2 Answers

Steven Parker
Steven Parker
229,644 Points

The "sleep" hours are initialized using a read-only static property named "Today" in the "Clock" class, but that property is never initialized nor is it a calculated property.

And the "Elapsed" event handler for the timer does not seem to ever get established, and the timer is never enabled.

Chris Kopcow
Chris Kopcow
2,852 Points

I remember I used DateTime.Today.AddHours initially, but that didn't work either. It still compiles, though. Either way, I'm not entirely sure how to initialize Today since its initial value would change depending on the day. I assumed that Today would just grab the info from the computer executing the program, and that would be that.

As for the timer, I enabled it when the pet is adopted, since I figured the timer doesn't much matter until that point, but I guess it could be enabled earlier. But I'm not sure how to set up the event handler? I see a lot of stuff in the documentation that refers to it, but I can't see where that would be established and what I'm doing wrong.

Steven Parker
Steven Parker
229,644 Points

You might establish "Today" as a computed property:

    private static DateTime Today => DateTime.Now.Date;

And when you set up the timer, you could establish the handler like this:

      timer.Elapsed += YourHandlerFunction;

The handler function should take 2 arguments, a plain "Object" and an "ElapsedEventArgs" object.

Chris Kopcow
Chris Kopcow
2,852 Points

Hmm, I implemented that computed property, and I'm still running into the same issue. Is it an issue with the Playtime() method?

Also, I think I set up the event handler properly, but I'm still getting the warning that the timer isn't assigned when I compile. What am I doing wrong?

Here's a new snapshot: https://w.trhou.se/g4p7azr5x2

Steven Parker
Steven Parker
229,644 Points

If you're local time is not UTC, your issue with "Playtime" might be the time zone. The workspace does not set the TZ environment variable by default. Try setting it manually when you run the program. I'm in central US, so I would run:
TZ=CST6CDT mono VirtualPet.exe

You have two timers, the static one "Timer" (capital "T") that currently is unused (thus the warning), and the instance "timer" (little "t") that seems to be working OK now.

Chris Kopcow
Chris Kopcow
2,852 Points

Ah, okay. I got rid of the static Timer, and that didn't seem to affect anything.

But I tried adjusting hours relative to UTC (I couldn't get the TZ= thing to work for EDT), but I still ran into the same "Come back at 1:30!" issue. Part of me wants to just take this whole section out, since it doesn't really matter to the functionality of the game other than educating myself on how to use DateTime, but I would really like to figure it out, and I don't understand why it's not working.

Steven Parker
Steven Parker
229,644 Points

Run it like this :point_right: TZ=EST5EDT mono VirtualPet.exe

If you still have trouble, you might try adding some code to print out the current, start, and end times so you can see why it thinks the time is out of the range.

Chris Kopcow
Chris Kopcow
2,852 Points

Yup! That worked! And I was able to get the timer working, so I'm all good now. Thank you so much for everything!