下面的程序执行没有结果#include<stdio.h>
#include<afx.h>VOID CALLBACK MyTimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
)
{
printf("ok");
}
void main()
{ UINT nTimerID; nTimerID=SetTimer(
    NULL,   // handle to window must set NULL
    NULL,      // timer identifier will be ignored.
    1000,           // time-out value
    (TIMERPROC)MyTimerProc   // timer procedure
    );
printf("%d",nTimerID);
}

解决方案 »

  1.   


    应该可以的,用GetLastError看看错误码.
      

  2.   

    SetTimer应该没有问题,但你的主函数体已经结束,线程关闭,SetTimer就会失效.
    作为你的程序,在SetTimer之后,应该至少有一个非死线程继续运行
      

  3.   

    请问楼上,具体该怎么做呢?
    是不是要写一个
    while(1)
    {
     ...
    }
      

  4.   

    加了 system("PAUSE");等待自己手动结束
      

  5.   

    给你一个包含定时器处理的经典线程处理模型:
    DWORD dwResult;
    HANDLE hEventArray[2];
    HANDLE hTimerEvent  = CreateEvent( NULL, FALSE, FALSE, NULL );

    hEventArray[0] = m_hExitEvent;
    hEventArray[1] = m_hTimerEvent;

    UINT nTID = timeSetEvent( FLUSH_TIMER, 1000, (LPTIMECALLBACK)hTimerEvent, 
    NULL, TIME_PERIODIC|TIME_CALLBACK_EVENT_SET );

    while ( m_bExitFlag ) 
    {
    dwResult = WaitForMultipleObjects( 2, hEventArray, FALSE, INFINITE );
    switch (dwResult) 
    {
            case WAIT_OBJECT_0 :
    timeKillEvent( nTID );
    CloseHandle( hTimerEvent);
    return 0;
    case WAIT_OBJECT_0 + 1:
    // 这里做你定时器需要做的处理
    break;
            case WAIT_FAILED:

                // A fatal error.  Pop up a message box and break out of
                // the while loop to end the thread.

      ::GetLastError());

                continue;

            case WAIT_IO_COMPLETION:

                // An I/O completion routine has been executed.  Cleanup
                // has already occurred in SendCompFunc, so there is
                // nothing left to do.  Just reiterate through the loop.

                continue;
    }
    }

    timeKillEvent( nTID );

    CloseHandle( hTimerEvent);
      

  6.   

    创建定时器,系统需要将WM_TIMER放入GUI线程的消息队列中,控制台程序中无消息队列,即使用回调函数,也是不成功的。