本人打算编制一个自动采集从摄象头那里采样的图象,采样的间隔时间和持续时间可以由用户自己设定,,我编的程序如下,但是并没有达到我的目的,请大虾指教!!!我需要声明的是:TimerProc回调函数应该没有错;
我不明白的是SetTimer(1,m_nElapsedTime,TimerProc);执行完一次采样工作以后,会不会
向后面的语句运行?其实我对SetTimer这个函数的运行规律都是不太明白,请大虾指教!!
另外,请大虾指出下面的函数能不能实现我的目的(应该是不能,我实践过的),如果不能,该怎么实现呢?
谢谢了!!void CDlgVideoTemp::OnBeginAutomaticlySave() 
{//这个类是对话框,上面有一个按纽,按下时将调用此消息函数
if(m_bIsFirstAtuomaticSampling)
{//记录下刚按下时的时间
m_FirstAtuomaticSampling = CTime::GetCurrentTime();
}
         //m_nElapsedTime为用户设定的间隔时间,TimerProc是回调函数,在此函数
         //中实现把剪贴板中的图象的句柄拷贝到veVideoImages中(全局函数)
SetTimer(1,m_nElapsedTime,TimerProc); m_bIsFirstAtuomaticSampling = FALSE;        //取得当前的时间
m_EndAutomaticSampling = CTime::GetCurrentTime();
        //获得现在与刚按下按纽时的时间差
CTimeSpan PersistingTime = EndAutomaticSampling - FirstAtuomaticSampling;
//如果此时间差大于用户设定的持续时间,则终止采样
if(PersistingTime > CTimeSpan(m_nPersistingTime))
{
KillTimer(1);
                 //把全局变量里面的图象的句柄拷贝到内部变量里面
for(int i = 0; i < veVideoImages.size(); i++)
{
m_vehVideoImages.push_back(veVideoImages[i]);
}
}
}

解决方案 »

  1.   

    先声明,偶不是大虾,地道菜鸟。
    SetTimer(1,m_nElapsedTime,TimerProc); m_bIsFirstAtuomaticSampling = FALSE;        //取得当前的时间
    m_EndAutomaticSampling = CTime::GetCurrentTime();
    SetTimer并不会立即跳到你的TimerProc中去,而是继续执行下面的语句。
    应该把后面的语句放到TimerProc中去,这样才能检测到时间变化。
    SetTimer其实就是启动定时器,每隔m_nElapsedTime往消息队列里扔个WM_TIMER,
    并不会中断当前的函数执行。
      

  2.   

    The System and the Timer
    The Windows timer is a relatively simple extension of the timer logic built into the PC's hardware and the ROM BIOS. Back in the pre-Windows days of MS-DOS programming, an application could implement a clock or a timer by trapping a BIOS interrupt called the "timer tick." This interrupt occurred every 54.925 msec, or about 18.2 times per second. This is the original 4.772720 MHz microprocessor clock of the original IBM PC divided by 218. Windows applications do not trap BIOS interrupts. Instead, Windows itself handles the hardware interrupts so that applications don't have to. For every timer that is currently set, Windows maintains a counter value that it decrements on every hardware timer tick. When this counter reaches 0, Windows places a WM_TIMER message in the appropriate application's message queue and resets the counter to its original value. Because a Windows application receives WM_TIMER messages through the normal message queue, you never have to worry about your program being "interrupted" by a sudden WM_TIMER message while doing other processing. In this way, the timer is similar to the keyboard and mouse: the driver handles the asynchronous hardware interrupt events, and Windows translates these events into orderly, structured, serialized messages. In Windows 98, the timer has the same 55-msec resolution as the underlying PC timer. In Microsoft Windows NT, the resolution of the timer is about 10 msec. A Windows application cannot receive WM_TIMER messages at a rate faster than this resolution—about 18.2 times per second under Windows 98 and about 100 times per second under Windows NT. Windows rounds down the time-out interval you specify in the SetTimer call to an integral multiple of clock ticks. For instance, a 1000-msec interval divided by 54.925 msec is 18.207 clock ticks, which is rounded down to 18 clock ticks, which is really a 989-msec interval. For intervals shorter than 55 msec, each clock tick generates a single WM_TIMER message.