Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:05:57+00:00 2026-05-23T21:05:57+00:00

I have a maze game and I’m trying to create two Timers at a

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:05:58+00:00Added an answer on May 23, 2026 at 9:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a maze game. After you press Enter, you can enter a cheat
My maze generator seems to have a problem. I am trying to generate something
I'm trying to write a simple maze game, without using any deprecated OpenGL API
I am new to OpenGL and was trying to create a simple maze that
I am creating a maze game. For that I have created 4 separate class
What I am doing is very simple. I have a simple 2d Maze game
I'm trying to use SharpDX to create a simple maze-like 2D program using DirectX.
i have to implement a small and simple game in c++ (a maze) and
Okay so I have two png files, a circle and a maze. Basically the
I have a program that is searching a maze to find the best way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.