Solved

timeSetEvent()

Posted on 1997-06-19
11
748 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
  • 6
  • 3
  • 2
11 Comments
 

Author Comment

by:Grub
Comment Utility
Edited text of question
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
How did you use timeSetEvent? Can you show the code?
0
 

Author Comment

by:Grub
Comment Utility
Edited text of question
0
 

Author Comment

by:Grub
Comment Utility
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
Comment Utility
It seems all ok. I've tested this code on NT4.0 and 95 - all works fine!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now