Thread: Dealy a function.

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Dealy a function.

    EDIT: I just saw I wrote Dealy instead of Delay in the thread name.

    Hello everyone.
    I know that I have been posting alot of questions here lately but for this question I cant find any other help.

    So anyway I have two functions, and I want one function to run, and then have the other one to run 2 seconds after the first one. The first thing I did was to use Sleep(2000);
    but for some reason that pauses the program at the beggining, The first function wont start either.

    Some of my code:
    Code:
    void functionone(HWND name)
    {
    SendDlgItemMessage(name,5,LB_ADDSTRING,0,(LPARAM)"Text One");	
    }
    
    void functiontwo(HWND name)
    {
    SendDlgItemMessage(name,5,LB_ADDSTRING,0,(LPARAM)"Text Two");	
    }
    void startfunc(HWND name)
    { 
    	functionone(name);
    	Sleep(2000);
    
    	functiontwo(name);
    }
    This startfunc is started when I press a button that I have(I'm using winApi).
    But the problem is that the first function wont start until 2 seconds after I press the button.
    Last edited by Danne; 01-14-2011 at 03:26 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first function is called.

    Then your whole program is put to sleep for two seconds.

    Then your second function is called.

    If you expect anything to happen in between any of those things (for instance, a message handler to receive and process your message), well, it won't.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void functionone(HWND name)
    {
    SendDlgItemMessage(name,5,LB_ADDSTRING,0,(LPARAM)"Text One");	
    }
    
    void functiontwo(HWND name)
    {
    SendDlgItemMessage(name,5,LB_ADDSTRING,0,(LPARAM)"Text Two");	
    }
    void startfunc(HWND name)
    { 
    	functionone(name);
    
            UpdateWindow(name);
    
    	Sleep(2000);
    
    	functiontwo(name);
    }
    It will still block your program for 2 seconds but should show the text in the first window.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    @tabstop:So the reason for the text not showing up is that the program pauses when the messages are being processed? Is there anything I can replace Sleep() with so that I can just delay the second function so that the messages from the first one gets recieved and processed before the second function is called?

    @commonTater: I am not sure that updating the window will work in the final code, as I am not just going to show text messages, but do other things too that needs to be delayed as well. if you understand what I mean.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I am not sure that updating the window will work in the final code, as I am not just going to show text messages, but do other things too that needs to be delayed as well. if you understand what I mean.
    If you want the text to show up right away, use UpdateWindow with the control's window handle in it... Otherwise there's no point delaying since you won't see anything until it's over, anyways.
    Last edited by CommonTater; 01-14-2011 at 03:18 PM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Alright, I used UpdateWindow. Works great. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM