Thread: calling a function after a delay

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    calling a function after a delay

    Hello all, it's my first post on this forum.

    I was wondering what is the best way to get an event to occur after a delay in Visual C++. Previously I have been using python and it's pretty easy to do there, using a command such as frame.after(X, myFunction) which would call myFunction after X milliseconds. What's the best way to do it in VC++?

    I've heard about the sleep function but I don't think that will work as the program I'm trying to write will need to be carrying on doing other stuff during the interval.

    Let's say, for a simple example, I want to write a program that basically counts to ten, so it prints "1" after 1 second, "2" after 2 seconds and so on. But I will need the program to remain "awake" during the delay, as there will also be a button on the screen that can be clicked on, which will result in "Response Made" whenever that happens, so if it is clicked in between 1 and 2 seconds I need to make sure the program is not sleeping.

    If anyone can give me any advice I'd be grateful! Thanks.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by James Greville View Post
    Let's say, for a simple example, I want to write a program that basically counts to ten, so it prints "1" after 1 second, "2" after 2 seconds and so on. But I will need the program to remain "awake" during the delay, as there will also be a button on the screen that can be clicked on, which will result in "Response Made" whenever that happens, so if it is clicked in between 1 and 2 seconds I need to make sure the program is not sleeping.
    Actually, your program doesn't need to be awaiting the mouse click between updates of your visual counter, at least in so far as it wont actually miss the click if you aren't listening for it right that microsecond. It would however cause a minor delay in responding to the click.
    The normal thing to do for something like this would be to run a timer, and under Windows at least you'd be listening for WM_TIMER events inside your event loop. Upon receiving one of those you'd invalidate and redraw part of the window.

    You should state what OS, and what UI frameworks (if any) you are using, for some specific help.

    Threading is another option which someone will no doubt mention, but that is something that even experts often get wrong, and besides it's probably overkill for what you want.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    Hey, thanks for the reply.

    Quote Originally Posted by iMalc View Post
    You should state what OS, and what UI frameworks (if any) you are using, for some specific help.
    I'm using Windows XP and Microsoft Visual C++ express to make a modification to the Half-Life 2 game.

    For the moment though, it might just be better to for me to try and understand timing within a simple hypothetical example, as the game is really complex.

    So let's just say on the screen I wanted two panels to appear. On the left hand side is a button, which the user can click on. Underneath this button is a text box. Whenever the button is clicked on, an "X" appears below it in the text box. On the right hand side is another box, which essentially is a countdown timer, starting at 10 and working its way to 0. When the timer reaches zero, no more responses are collected and no more "X's" will appear in the textbox.

    It's the timing part of this program that I would struggle with. Obviously what I need the program to do is after 1s, delete the 10 and replace with a 9, and so on.

    It's this basic idea of making something happen after a delay that I want to then try and use in HL2. I want to call a function, but rather than it be called immediately, be called after a specific delay. But I thought that if I used sleep(1000) then the program would not be doing anything while it was sleeping. Obviously in Half-Life 2 there would need to be all sorts of things going on during any interval so this wouldn't work.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    Or here's an even simpler suggestion, how can I write a program that will print "hello world 10 seconds later" after a delay of 10 seconds from when the program is launched, but without using sleep()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a delay function
    By ThLstN in forum C Programming
    Replies: 5
    Last Post: 01-06-2008, 02:54 PM
  2. Is there a delay function?
    By Digga in forum Windows Programming
    Replies: 2
    Last Post: 05-06-2005, 10:56 AM
  3. Delay function
    By knight543 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2002, 03:04 PM
  4. Use a delay function
    By Robert_Ingleby in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 06:58 AM
  5. time delay function available?
    By cUser in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 12:14 AM