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 8850829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:59:26+00:00 2026-06-14T12:59:26+00:00

Question : How do you make a timer tick in the background? That is

  • 0

Question : How do you make a timer tick in the background? That is the thread that create the timer thread can still do something else while clock is ticking.

Attempt:
-Using _beginthreadex() –> It seems to have race condition

class Timer{
 ...
 static unsigned __stdcall tick(void *param){
    while(1){
        Timer::timer++;
        Sleep(Timer::timer*1000);
    }
    return 1;
}
}

.....
HANDLE time_thread = (HANDLE) _beginthreadex(0, 0, &Timer::tick,0,0,NULL);
...
//test for 20 seconds
//want to do something while the clock is not 20 seconds
//the mainthread here still has to receive input
//What is the proper way to do it?

while (Timer::getTime() != 20){
   cout << Timer::getTime()
}

CloseHandle(time_thread);
...

NOTE: Iam using Visual Studio 2008, not 11 so I do not have C++11 support.

  • 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-06-14T12:59:28+00:00Added an answer on June 14, 2026 at 12:59 pm

    I’m not sure what’s wrong with what you have here. You’ve created a thread that updates a member variable timer forever and your main use of it is a tight/fast loop that prints (presumably) that time until it reaches 20. What is it not doing? Technically there’s a race condition of incrementing that value versus checking it in another thread, but for the purposes of this example it should be fine…

    EDIT: try this for non-blocking input with full input control:

    HANDLE hStdIn = GetStdHandle( STD_INPUT_HANDLE );
    while ( true ) {
        if ( WAIT_OBJECT_0 == WaitForSingleObject( hStdIn, 1000 ) ) {
            // read input
            INPUT_RECORD inputRecord;
            DWORD events;
            if ( ReadConsoleInput( hStdIn, &inputRecord, 1, &events ) ) {
                if ( inputRecord.EventType == KEY_EVENT ) {
                    printf( "got char %c %s\n",
                        inputRecord.Event.KeyEvent.uChar.AsciiChar,
                        inputRecord.Event.KeyEvent.bKeyDown ? "down" : "up" );
                }
            }
        }
        printf( "update clock\n" );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a background thread on the iPhone that executes some code
Dumb question time: In the following request spec, I try to make sure that
Ok, so I'll try to make that question a little more simple. The core
My question is when the timer is start ticking. Let's take for example, timer
I'm writing an app that will need to make use of Timer s, but
Question regarding clock tick count generated by clock() from <ctime> . (Usage of clock()
I want a timer tick/elapsed event inside a new thread. It seems like I
I have a basic question. I would like to make running Arc.py several times
After reading answer to this question: Make "make" default to "make -j 8" I
There's another question: howto make diff look like svn diff? but I want the

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.