Solved

timeSetEvent()

Posted on 1997-06-19
11
768 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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