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

  • Home
  • SEARCH
  • 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 7661333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:03+00:00 2026-05-31T13:35:03+00:00

#include stdafx.h #include <stdio.h> #include <iostream> #include <time.h> using namespace std; using namespace System;

  • 0
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;
using namespace System;

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock() + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
void timer()
{
    int n;
    printf ("Start\n");
    for (n=10; n>0; n--) // n = time
    {
        cout << n << endl;
        wait (1); // interval (in seconds).
    }
    printf ("DONE.\n");
    system("PAUSE");
}
int main ()
{
    timer();
    cout << "test" << endl; // run rest of code here.}

  return 0;
}

I’m trying to create a timer in C++ which would run in the background. So basically if you’d look at the ‘main block’ I want to run the timer (which is going to count down to 0) and at the same time run the next code, which in this case is ‘test’.

As it is now the next line of code won’t be run until the timer has finished. How do I make the timer run in the background?

Thanks for your help in advance!

  • 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-31T13:35:04+00:00Added an answer on May 31, 2026 at 1:35 pm

    C++11. Should work with VS11 beta.

    #include <chrono>
    #include <iostream>
    #include <future>
    
    void timer() {
        std::cout << "Start\n";
        for(int i=0;i<10;++i)
        {
            std::cout << (10-i) << '\n';
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        std::cout << "DONE\n";
    }
    int main ()
    {
        auto future = std::async(timer);
        std::cout << "test\n";
    }
    

    If the operation performed in timer() takes significant time you can get better accuracy like this:

    void timer() {
        std::cout << "Start\n";
        auto start = std::chrono::high_resolution_clock::now();
        for(int i=0;i<10;++i)
        {
            std::cout << (10-i) << '\n';
            std::this_thread::sleep_until(start + (i+1)*std::chrono::seconds(1));
        }
        std::cout << "DONE\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

include stdafx.h #include <iostream> using namespace std; class Foo{ public: void func() { cout<<Hello!!<<endl;
#include <iostream> using namespace std; int main() { double u = 0; double w
//#include StdAfx.h #include <stdio.h> #include <windows.h> #include <winbase.h> #include <iostream> #include <tchar.h> using namespace
project1.cpp #include stdafx.h #include Bicycle.cpp using namespace std; int _tmain(int argc, _TCHAR* argv[]) {
#include stdafx.h #include stdio.h #include math.h #include stdlib.h void test (int a,int *b, int
The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()
#include <stdafx.h> #include <stdio.h> #include <conio.h> #include<stdlib.h> #define My_Sizeof(type) ((char*)((type*)0 +1) - (char*)(type*)0) void
#include stdafx.h #include stdio.h #include math.h int main() { float c; printf(Type c); scanf(%f,
#include stdafx.h #include <windows.h> #include <winsock.h> #include <stdio.h> int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int
#include stdafx.h #include<stdio.h> #define PR(x) printf(%d\t,(int)(x)); #define PRINT(a,b,c) PR(a) PR(b) PR(c) #define MAX(a,b) (a<b?b:a)

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.