0

I have a maze game and I'm trying to create two Timers at a time. 1st (Exits the game after 300 secs)

t1.Interval = 30000;
t1.Enabled = true;
t1.Elapsed += new ElapsedEventHandler(hiddenTimer);

public static void hiddenTimer(object source, ElapsedEventArgs e)
    {
        Console.Clear();
        Environment.Exit(1);
    }

2nd (Displays the time remaining every 1 sec (like a real timer))

t2.Interval = 1000;
t2.Enabled = true;
t2.Elapsed += new ElapsedEventHandler(showTimer);

public static void showTimer(object source, ElapsedEventArgs e)
    {
        Console.Write(timeLeft);
    }

I would want to pass declare timeLeft globally but it says that "An object reference is required for the non-static field, method, or property..."

How would I declare it properly?

Reinan Contawi
  • 225
  • 1
  • 6
  • 13
  • 1 sec == 1000 msecs, so t1.Interval=300000, add one 0 – Tigran Jul 15 '11 at 18:15
  • Please post all of your code. You did not post the declaration of timeLeft. The problem is obvious your trying to use a non-static property within a static method. – Security Hound Jul 15 '11 at 18:24
  • Both of my timers are working but i have a problem displaying the 2ndTimer because it's a maze game, My cursor keeps moving that's why the Time follows my cursor. any suggestion how would i fix this? – Reinan Contawi Jul 15 '11 at 18:25
  • How are you writing the time? Are you actually using Console.WriteLine? – Ed Bayiates Jul 15 '11 at 18:36
  • Then when you go to write the time, save the current position, move the position to where you want to display the time, write the time, then restore the position. – Ed Bayiates Jul 15 '11 at 18:40
  • it's too fast. now my avatar 'Ö' is leaving trails. – Reinan Contawi Jul 15 '11 at 19:07

4 Answers4

7

By making a static property:

public static Double TimeLeft { get; set; }

This is if you want to Publicliy accessable from your entire context, if you want it private, just change public to private.

Just a side note, the built in Timer doesn't support polling for the remaining time until the next elapse. Either you decrease TimeLeft in each Elapse-event on the 1sec timer or you can have a look at this.

Edit

Here is one way to do it with one timer, first I declare two properties and one constant field that I use, don't bother that they are static, it's just easier to run it as a console application this way.

public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }

private const double CycleInterval = 1000;

Then in my Main-method I have the following to initiate my Timer

SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;

SystemTimer.Start();

Having this, the Cycle-event handler can look like this:

static void Cycle(object sender, ElapsedEventArgs e)
{
    Elapsed += CycleInterval;

    if ((Elapsed%5000) == 0.0)
    {
        Console.WriteLine("5 sec elapsed!");
        // Do stuff each 5 sec
    }

    if ((Elapsed % 10000) == 0.0)
    {
        Console.WriteLine("10 sec elapsed!");
        // Do stuff each 10 sec
    }

    Console.WriteLine("Elapsed: {0}", Elapsed);
}

You could also have Elapsed being a TimeSpan, but you can refactor this as you like.

Here's my complete source code that I used:

using System;
using System.IO;
using System.Timers;

namespace ConsoleApplication5
{
    class Program
    {
        public static Timer SystemTimer { get; set; }
        public static double Elapsed { get; set; }

        private const double CycleInterval = 1000;

        static void Main(string[] args)
        {
            SystemTimer = new Timer();
            SystemTimer.Interval = CycleInterval;
            SystemTimer.Enabled = true;
            SystemTimer.Elapsed += Cycle;

            SystemTimer.Start();

            while (true) ;

        }

        static void Cycle(object sender, ElapsedEventArgs e)
        {
            Elapsed += CycleInterval;

            if ((Elapsed%5000) == 0.0)
            {
                Console.WriteLine("5 sec elapsed!");
                // Do stuff each 5 sec
            }

            if ((Elapsed % 10000) == 0.0)
            {
                Console.WriteLine("10 sec elapsed!");
                // Do stuff each 10 sec
            }

            Console.WriteLine("Elapsed: {0}", Elapsed);
        }
    }
}

And this is what it looks like when I run it:

enter image description here

Community
  • 1
  • 1
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • yeah. i already added a timeLeft--; in the showTimer() method. my problem now is how to write the time. my cursor keeps moving sice it's a maze game that's why my cursor leaves a trail of time! lol. i know this is an another problem, but please help me if you can. :D – Reinan Contawi Jul 15 '11 at 18:31
  • @Reinan, Updated my answer again with an example on how to do it with one timer, might be easier to read your code in the end instead of having a lot of timers. But keep in mind, this is just one way, using a `TimeSpan` is also a nice way to do it. – Filip Ekberg Jul 15 '11 at 18:37
  • @Reinan, I don't quite follow you on the other problem. If your initial problem is now solved, mark the answer that helped you solve your problem as Accepted. Then create a new question with the other problem. – Filip Ekberg Jul 15 '11 at 18:38
  • @Filip I'm sorry i'm new here i'm not really used to how things work here :D – Reinan Contawi Jul 15 '11 at 18:43
  • i would post another question, please take a look at my post later one. – Reinan Contawi Jul 15 '11 at 18:45
  • http://stackoverflow.com/questions/6711853/c-fixing-timer-in-a-place please help me with this one – Reinan Contawi Jul 15 '11 at 18:54
  • @Reinan, I gathered that you are new, and Welcome to StackOverflow :) – Filip Ekberg Jul 15 '11 at 19:08
2

First of all, you should declare your timeLeft as a static if you want it to behave like a global variable.

Secondly I'd use one timer and keep track of the time separately for each event:

static DateTime startTime = DateTime.Now;
static DateTime lastTime = DateTime.Now;

In your timer, which should be set to something to give more accuracy like 1/10 of a second, do this:

if (DateTime.Now - lastTime > new TimeSpan(0, 0, 1))
   // Update the time here for your 1s clock
lastTime = DateTime.Now;

if (DateTime.Now - startTime > new TimeSpan(0, 0, 300))
    // Exit the game

Your timings will be more accurate this way.

Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • how would i suppose to do that? any suggestions? – Reinan Contawi Jul 15 '11 at 18:22
  • @Filip, that's funny you would be so competitive to post that comment on my answer. – Ed Bayiates Jul 15 '11 at 18:34
  • that would be helpful but i have a new problem. can you help me display the Time in one place only? the time follows the track of my cursor which i am using to move the avatar inside the maze. – Reinan Contawi Jul 15 '11 at 18:35
  • @ Filip @ Ares your revisions are both working. thanks you very much. i would be more grateful i you could help me with my another problem. :D – Reinan Contawi Jul 15 '11 at 18:36
  • @AresAvatar, Comment removed, I did not mean to offend by saying that I also made an example of it. I was already in Visual Studio doing it when I saw that he wrote "how would I do that". – Filip Ekberg Jul 15 '11 at 18:39
  • @Filip, you didn't offend me, I thought it was funny. I love the competitiveness on StackOverflow. – Ed Bayiates Jul 15 '11 at 18:41
  • @AresAvatar, "competition" is what drives good answers. :) I honestly didn't mean it like that though, I just want to help out. – Filip Ekberg Jul 15 '11 at 19:08
  • @Filip: I agree, that's why I love it. You obviously help out a lot -- you have a very high reputation! – Ed Bayiates Jul 15 '11 at 19:29
0

Mark it static:

public static int TimeLeft;
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
0

Your timeLeft memeber is not static.

Make it static or make showTimer method non static.

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123