Solved

timeSetEvent()

Posted on 1997-06-19
11
789 Views
Last Modified: 2013-12-03
I have examples of seting up a timer and have followed the code exactly. I am not calling it from MFC DLL InitInstance(). When my program runs it hangs when it tries to set up the timer. When I comment timeSetEvent() out it works (without a timer though and I need one).I made the CALLBACK procedure do nothing and it still didn't work. I use Borland C++ 5.01 and programming for Windows 95. I am at a loss, please help./*
      the timer function
*/
void CALLBACK TimerFunc( UINT wID, UINT wUser, DWORD dwUser, DWORD dw1, DWORD dw2 )
{
        if( PostPending == FALSE )
     {
                 PostPending = TRUE;
         PostMessage( hWndMain, WM_USER, 0, (DWORD)0 );
     }
     else
     {
                 timerTicksMissed++;
     }
        UNREFERENCED_PARAMETER(dw1);
     UNREFERENCED_PARAMETER(dw2);
     UNREFERENCED_PARAMETER(dwUser);
     UNREFERENCED_PARAMETER(wUser);
     UNREFERENCED_PARAMETER(wID);
} // TimerFunc

static void SetTime( void )
{
    TIMECAPS       caps;

    timeGetDevCaps( &caps, sizeof(caps) );
    ticksPerSecond = 1000 / caps.wPeriodMin;

    timeBeginPeriod( 1000 / ticksPerSecond );

    timerid = timeSetEvent( ( 1000 / ticksPerSecond ), ( 1000 / ticksPerSecond ), TimerFunc, (DWORD) 0, (UINT) TIME_PERIODIC );


}
0
Comment
Question by:Grub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 

Author Comment

by:Grub
ID: 1398635
Edited text of question
0
 
LVL 23

Expert Comment

by:chensu
ID: 1398636
How did you use timeSetEvent? Can you show the code?
0
 

Author Comment

by:Grub
ID: 1398637
Edited text of question
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:Grub
ID: 1398638
sorry I didn't include the code the first time around. I hope it helps you answer my question.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398639
It seems all ok. I've tested this code on NT4.0 and 95 - all works fine!
0
 
LVL 23

Expert Comment

by:chensu
ID: 1398640
It seems that there is nothing wrong with the code showed here. You can reduce your code to find out where the problem is. You can try the following code. Simply create a window, call SetMMTimer() when receiving WM_CREATE and ReleaseMMTimer() when receiving WM_DESTROY. You may need to modify some code (VERIFY, AfxMessageBox) for Borland C++.


DWORD m_dwCount;
UINT m_nRes;
UINT m_nMMTimerID;

//-----------------------------------------------------------------------------
BOOL SetMMTimer()
{
    m_dwCount = 0;

    TIMECAPS caps;
    VERIFY(::timeGetDevCaps(&caps, sizeof(caps)) == TIMERR_NOERROR);

    m_nRes = caps.wPeriodMin;

    VERIFY(::timeBeginPeriod(m_nRes) == TIMERR_NOERROR);

    m_nMMTimerID = ::timeSetEvent(m_nRes, m_nRes,
                                TimeProc,
                                (DWORD)&m_dwCount,
                          TIME_PERIODIC);
      
    if (m_nMMTimerID == 0)
      ::AfxMessageBox("timeSetEvent failed.");

    return TRUE;
}
//-----------------------------------------------------------------------------
void ReleaseMMTimer()
{
    if (m_nMMTimerID != 0)
    {
      // kill the periodic timer event
      VERIFY(::timeKillEvent(m_nMMTimerID) == TIMERR_NOERROR);
      m_nMMTimerID = 0;
    }

    VERIFY(::timeEndPeriod(m_nRes) == TIMERR_NOERROR);

    CHAR chBuf[16];
    ::wsprintf(chBuf, "%lu", m_dwCount);
    ::AfxMessageBox(chBuf);
}
//-----------------------------------------------------------------------------
void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser,
                   DWORD dw1, DWORD dw2)
{
    (*(LPDWORD)dwUser)++;
}
//-----------------------------------------------------------------------------

0
 

Author Comment

by:Grub
ID: 1398641
Sorry chensu but I tried the code you gave me (with changes) and it still hangs. The code examples I have say that WINMM.LIB should be linked in, however the fact that it compiles means that it is being linked in automatically, right? I couldn't find WINMM.LIB anywhere on my harddrive (only the DLL) so I created one using IMPLIB and linked that in, no luck still hangs. Maybe I should try and get WINMM.LIB ?
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1398642
For Visual C++, winmm.lib is not linked by default. Anyway, winmm.lib should be there. It is likely that there is something wrong with your compiler. You'd better reinstall it. The code I showed you works fine on my computer.
0
 

Author Comment

by:Grub
ID: 1398643
By coincidence I just finished reinstalling Borland C++ 5.01 and then did a search for winmm.lib. Still not found except for the one I created using ImpLib. I might have a look on the Borland Site. Thanks for your help anyway.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1398644
Borland C++ does not use separate Window's import libraries. It just uses the single large import32.lib . Import32.lib includes winmm.lib .
0
 

Author Comment

by:Grub
ID: 1398645
Thanks for all your help, but I have worked out why the code wasn't working. It does. I ran the executable without from explorer and it worked fine with the timeSetEvent() in. I have come to the conclussion that Borland somehow dissables timers that call a callback function. When I want to run my program I have to compile it without running it and then exit borland and run it from explorer. If anyone knows why Borland exibits this behaviour I would like to know how to stop it.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question