CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: Problem With Sleep() When drawing ananalog clock

  1. #1
    Join Date
    May 2014
    Posts
    72

    Problem With Sleep() When drawing analog clock

    Hi,

    The question says:

    Make an "analog clock" that is, a clock with hands that move. You get the time of day from the operating system through a library call. A major part of this exercise is to find the functions that give you the time of day and a way of waiting for a short period of time (e.g., a second for a clock tick) and to learn to use them based on the documentation you found. Hint: clock(), sleep().


    OK, I wrote below code. It is in its primary stages and has not been completed yet.

    Code:
    #include <GUI.h>
    #include <time.h>
    #include <iostream>
    using namespace Graph_lib;
    
    //---------------------------------
    
    class Dynamic_clock : public Window {
    
    public:
    	Dynamic_clock(Point p, int w, int h, const string& title):
    		Window(p, w, h, title),
    		quit_button(Point(x_max()-90,20), 60, 20, "Quit", cb_quit),
    		l1(Point (p.x+100,p.y), Point(p.x+120,p.y)),
            l2(Point (p.x-100,p.y), Point(p.x-120,p.y)),
    		l3(Point (p.x,p.y-100), Point(p.x,p.y-120)),
    		l4(Point (p.x,p.y+100), Point(p.x,p.y+120)),
    		hour1(Point(300,250), Point(300,155)),
    		hour2(Point(300,250), Point(310,165)),
    	    c(Point(p), 120) {
    			attach(c);
    			attach(l1);
    			attach(l2);
    			attach(l3);
    			attach(l4);
    			attach(quit_button);	
    			clock_hands();
    	}
    
    private:
    	Button quit_button;
    	Circle c;
    	Line l1, l2, l3, l4, hour1, hour2;
    	double h, m, s;
    
    	void quit() { hide(); }
    
    	void clock_hands() {
    		get_current_time();
    		hour1.set_style(Graph_lib::Line_style(Graph_lib::Line_style::solid, 3));
    		attach(hour1);
    		Sleep(1000);
    	        detach(hour1);
    		attach(hour2);
    		
    	}
    	void get_current_time() {
    		time_t t = time(0);
    		t -= 1421526600;
    		double h = t/3600;
    		t -= h*3600;
    		double m = t/60;
    		t -= m*60;
    	    double s = t;
    	}
    
    
    	static void cb_quit(Address, Address pw) {reference_to<Dynamic_clock>(pw).quit();}
    };
    
    //------------------
    
    int main() {
    		Dynamic_clock dc(Point(300,250), 800, 600, "Dynamic_clock"); 
    		return gui_main();
    }
    Now what is the problem?

    I expect the system in void clock_hands() (line 38) attaches hour1 (line 41) then waits for 1000 ms (using Sleep(1000)) then detaches hour1 and attaches hour2 this time. But it doesn't occur in practice. When the system reaches Sleep(1000); it seems to go into a comma! It doesn't show the hour1 so seeing the movement of clock ticks by the clock's hands will not be possible.

    Any idea please?
    Regardless of my code, how you would solve the problem if you were me?
    The problem is an exercise. #6 of here: http://books.google.com/books?id=We2...epage&q&f=true
    Last edited by abbassi; January 20th, 2015 at 02:20 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,734

    Re: Problem With Sleep() When drawing ananalog clock

    Quote Originally Posted by abbassi View Post
    ...
    Now what is the problem?

    I expect the system in void clock_hands() (line 38) attaches hour1 (line 41) then waits for 1000 ms (using Sleep(1000)) then detaches hour1 and attaches hour2 this time. But it doesn't occur in practice. When the system reaches Sleep(1000); it seems to go into a comma! It doesn't show the hour1 so seeing the movement of clock ticks by the clock's hands will not be possible.

    Any idea please?
    You should not use Sleep in a main GUI thread. Why don't you want use the timer instead?

    Quote Originally Posted by abbassi View Post
    ...
    Regardless of my code, how you would solve the problem if you were me?
    The problem is an exercise. #6 of here: http://books.google.com/books?id=We2...epage&q&f=true
    And where exactly in this book could we find this "exercise. #6"?
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: Problem With Sleep() When drawing ananalog clock

    Maybe I'm missing something, but shouldn't there be some kind of loop in there?

    And I agree with VictorN, I'd be using a timer.

  4. #4
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing ananalog clock

    Thanks for the reply.
    What do you mean by main GUI thread please? The main() function?
    The exercise itself recommend to use of clock() and sleep(), but if you know any other good tool please tell me of it. I haven't used timers till now and don't know how to as well.

    About the exercise, if you click on the link I have given, it takes you to the desired page and there are numbers of questions there. Question number 6 is the one I say.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,734

    Re: Problem With Sleep() When drawing ananalog clock

    Quote Originally Posted by abbassi View Post
    About the exercise, if you click on the link I have given, it takes you to the desired page and there are numbers of questions there. Question number 6 is the one I say.
    It took me to the page between the book title and the Contents
    Victor Nijegorodov

  6. #6
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing analog clock

    I don't know what is the case with the link but it works properly for me.
    Anyway, the exercise no.6 says just what I wrote at first post of here: Make an "analog clock" that is, a clock ....
    Last edited by abbassi; January 20th, 2015 at 02:20 AM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: Problem With Sleep() When drawing ananalog clock

    Quote Originally Posted by abbassi View Post
    I don't know what is the case with the link but it works properly for me.
    Anyway, the exercise no.6 says just what I wrote at first post of here: Make an "analog clock" that is, a clock ....
    The library you are using is creating a Windows GUI app under the covers in spite of you simply declaring a class and calling a method in the main() function.

    Given that this is now a Windows app, you have to follow the rules of a Window's app and that means "don't block the UI thread". So you can't use Sleep() in the main UI thread.

  8. #8
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing ananalog clock

    May you read the exercise once again please? The exercise is of the book Programming Principle and practice using C++, by Stroustrup.
    If you were me, how would you solve the exercise so?

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Problem With Sleep() When drawing ananalog clock

    you cannot possibly make a visible clock by.

    drawing the time
    wait 1 sec
    draw the Original time +1sec
    wait 1 sec
    draw the Original time +2sec
    etc...

    as the orinal question seems to indicate.
    Trying to make it like that, your clock will invariably start to drift very rapidly and be incorrect by several seconds even after only a few minutes of running.

    what you want to do is create a timer that runs faster than the visible clock.
    In the timer handle, you get the time, if it has changed from the displayed time, you update the visible clock.
    you typically want the timer to run at least twice as fast as the smallest visible increment (1second ?), but something like 10x is probably even better (much more than that isn't really going to matter much)

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Problem With Sleep() When drawing analog clock

    Quote Originally Posted by abbassi View Post
    Now what is the problem?

    I expect the system in void clock_hands() (line 38) attaches hour1 (line 41) then waits for 1000 ms (using Sleep(1000)) then detaches hour1 and attaches hour2 this time. But it doesn't occur in practice. When the system reaches Sleep(1000); it seems to go into a comma! It doesn't show the hour1 so seeing the movement of clock ticks by the clock's hands will not be possible.
    Calling sleep pretty much puts your program (well, the thread that calls sleep) into a coma. It doesn't do anything until it wakes from its sleep. One important thing your program won't do while it calls sleep is redrawing the user interface. This is why you shouldn't call sleep or any other function that could take a long time from your main thread. The solution is to either use a worker thread or a timer.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  11. #11
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing ananalog clock

    @OReubens :

    Thank you for your explanations.

    What I got from your reply were:
    1- Solving the exercise by the materials it has said is not possible. And it's the author's mistake that has had that question. Am I right?
    2- I need to use of a C++ timer rather than clock() and/or Sleep(). And there isn't any function named sleep(). What the author has mentioned.

    If so, OK I try to use of timers instead. But I have never ever used them. I searched the web for that but mostly it takes me to MSDN..... website that I can't understand the contents (probably I'm too novice).
    If timer is best solution, do you know simplest way for learning how to use it please?
    Last edited by abbassi; January 20th, 2015 at 09:54 AM.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: Problem With Sleep() When drawing ananalog clock

    Quote Originally Posted by abbassi View Post
    Thank you for your explanations.

    What I got from your reply were:
    1- Solving the exercise by the materials it has said is not possible. And it's the author's mistake that has had that question. Am I right?
    2- I need to use of a C++ timer rather than clock() and/or Sleep(). And there isn't any function named sleep(). What the author has mentioned.

    If so, OK I try to use of timers instead. But I have never ever used them. I searched the web for that but mostly it takes me to MSDN..... website that I can't understand the contents (probably I'm too novice).
    If timer is best solution, do you know simplest way for learning how to use it please?
    Not really. What's being said is that sleep will work, but it's not good programming practice. You'd be better to have a timer fire every second to update your clock than to use sleep which essentially tells the operating system to pause your program. When you use sleep, your program, or at least that thread won't respond to users at all, so it's not a good habit to get in to.

    I'll ask the question I asked before. I would expect some kind of loop so that it displays the time, goes to sleep and displays the time again over and over again. I don't see a loop like that.

  13. #13
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing ananalog clock

    The post #11 was for OReubens.

    @GCDEF:
    About the loop you are right. But as I said in my first post of this thread, "It is in its primary stages and has not been completed yet." I would want to be sure that this sleep() and clock() work as I expected then complete the program.

    So you guys are all agree to don't use of Sleep() (or sleep()). And agree to use of a timer or fire times is better. OK.
    Please recommend a simple/easy way of being familiar with timer or fire timers. They are new for me.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: Problem With Sleep() When drawing ananalog clock

    Start here
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    I believe your initial problem is because you don't have a loop, or it may just be that your program is going to sleep before the hands have a chance to draw.

  15. #15
    Join Date
    May 2014
    Posts
    72

    Re: Problem With Sleep() When drawing ananalog clock

    I also made a loop into void clock_hands() but same result. Sleep() takes to comma and is not good for my situation.
    I don't know why I can't easily understand the msdn touts but I try to figure out what is and how to set a timer. Thank you very much.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)