CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Timer & Thread

  1. #1
    Join Date
    Dec 2002
    Location
    India
    Posts
    23

    Timer & Thread

    Sir,

    I'm developing a Real time application program.
    Based on the time i'm taking data from the database &
    processing. So once the program is started, all the processes
    are executed on the basis of time. So i want to get the time
    at any time during the exexution. If i'm using the SetTimer() function i can't get that.
    I'm using a PCI card(I/O) to send & recieve signals from
    the external devices. I want to poll each external devices.
    No i'm planning to use thread for that. Is there any other method for that.

    kasi
    Kasi P.Pillai
    SMEC Automation PVt. Ltd.
    Kochi,
    Kerala,
    India.
    e-mail->[email protected]

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...which operating system you are using? It looks like you are trying to achieve this under Windows.

    First of all Windows is far away from being real-time. So, if you definitely need absolute real-time you should consider using such an operating system like QNX.

    Nevertheless there are much better solutions under Windows than using the WM_TIMER messages like waitable timers or multimedia timers...

    The following is a sample how to use waitable timers. They will delay execution by 10 seconds in the following...
    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    int main()
    {
      HANDLE hTimer = NULL;
      LARGE_INTEGER liTime;
    
      liTime.QuadPart=-100000000;
    
      // Create a waitable timer.
      hTimer = ::CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
      if(!hTimer)
      {
        std::cout << "Could not create waitable timer (Error "
                  << ::GetLastError() << ")" << std::endl;
        ::Sleep(5000);
        return -1;
      }
    
      std::cout << "Wait for 10 seconds..." << std::endl;
    
      // Set timer
      if(!::SetWaitableTimer(hTimer, &liTime, 0, NULL, NULL, 0))
      {
        std::cout << "Could not set waitable timer (Error "
                  << ::GetLastError() << std::endl;
        ::Sleep(5000);
        return -1;
      }
    
      // Wait for timer
      if(::WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
        std::cout << "Could not wait for timer object (Error "
                  << ::GetLastError() << std::endl;
      else
        std::cout << "Time elapsed" << std::endl;
    
      // Wait for keystroke
      _getch();
    
      return 0;
    }
    For additional information you might take a look at http://www.codeproject.com/system/timers_intro.asp. It is a very comprehensive timer tutorial.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured