Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Timer code that doesnt interupts proceedure  (Read 3224 times)

0 Members and 1 Guest are viewing this topic.

Elexir

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Timer code that doesnt interupts proceedure
    « on: January 04, 2012, 11:17:11 AM »
    How can i write a timer code like the 1 in Visual Basic
    Timer code must be in c++
    at the moment i am trying to build a game where i am using a timer for running actions
    1.This timer code cannot interupts other code proceedue
    2.Logical function and other process will be place inside this timer code

    i try writting it but when that timer function is running... my other proceedure stops
    I know this is because the process stops and it can only be running again when timer code has finish processing
    So how can i avoid having other proceedure stop running

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Timer code that doesnt interupts proceedure
    « Reply #1 on: January 04, 2012, 12:41:21 PM »
    Use a thread, not a timer. Of course you don't present any information on what your target platform is, which makes it a bit difficult to offer much more advice than that.

    General advice:

    1. Don't use a timer.
    2. Don't use a timer.
    3. And most importantly- don't use a timer.

    A GameProc() routine should run as fast as it possibly can, on a separate thread. It will grab a critical section/mutex, update game state, move objects, etc.  Release the mutex/critical section. Each iteration it should post a message to the main thread (which, for most operating systems would be the one managing the UI) to tell it to repaint, Otherwise, the game loop is throttled by the delay the timer has, which for most systems is at least 53 or so milliseconds. The Paint Message handler (or equivalent) would then deal with grabbing a critical section on the various game objects as needed and drawing with them. Depending on the way the gameproc() messes with the gamestate variables sometimes you can shorten critical sections so that the painting thread can get a word in edgewise.
    I was trying to dereference Null Pointers before it was cool.

    Elexir

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Timer code that doesnt interupts proceedure
      « Reply #2 on: January 04, 2012, 06:31:06 PM »
      Thanks for replying

      My target platform is window PC
      I dont know what you mean by use a thread but i will look it up
      Thanks