Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,567 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,645 people online right now. Registration is fast and FREE... Join Now!




Stoping a timer

 
Reply to this topicStart new topic

Stoping a timer

Moezzie
25 Nov, 2007 - 03:55 AM
Post #1

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 47


My Contributions
Im currently coding a very basic c++ timer app witch is supposed to output the elapsed time since the start button was clicked.
Im using a label and two buttons(start and stop) from the Gtkmm libraries.

The problem is that the app freezes up during the time my wait function is running. This might seem pretty obvious but i had hoped to be able to stop the timer any time. Once the wait function is finished everything works fine again.
Is there a better way to get the output once every second and still be able to perform other actions during that time?

Im using the Gtkmm libraries to do the graphical stuff, i dont know if the problem could be connected to that somehow.

Anyway. These are the important parts of my code:
CODE

void myWindow::wait (int seconds )
{
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC;
    while (clock() < endwait) {}
}

void myWindow::on_start_clicked()
{
    go = true;
    start = time(NULL);

    while(go == true)
    {    
        end = time(NULL);
        secs = difftime(end,start);
        std::cout <<"Elapsed time: "<< secs << "\n";
        myWindow::wait(1);
    }    
}

void myWindow::on_stop_clicked()
{
    go = false;
}


Thanks for your time.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month