4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Salem
0

How to make a time based regular function?Like something that plays a sound every 1 second?

well, with the information that you provide this could work:

#include <iostream>

using namespace std;

void doSomething();

int main()
{
    int waittime = 5; // 5 seconds

    while(1)
    {
        doSomething();

        sleep(waittime);
    }
}

void doSomething()
{
    cout << "something done!" << endl;
}
0

>How to make a time based regular function?
>Like something that plays a sound every 1 second?

To what end? If all you want to do is hog the process and play a sound every second, it's a trivial matter:

#include <iostream>
#include <windows.h>

void kindadumb()
{
  for ( ; ; ) {
    std::cout<<'\a';
    Sleep ( 1000 );
  }
}

int main()
{
  kindadumb();
}

But if you want something to happen while that function is running, you get into troublesome code such as faking multithreading, using actual threads, or spawning a separate process. How you're going to use the function plays a big part in how it needs to be implemented.

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.