MFC延迟响应函数

1.推荐用Sleep()
Sleep() 函数头文件为 <windows.h>
MS VC++可以用MFC的Sleep函数,参数是毫秒。


#include<iostream>
#include<windows.h>
using namespace std;

void main ()
{
      Sleep(1000);     //延时1秒
      cout<<"adsd"<<endl;
      Sleep(10000);       // 注意S大写
      cout<<"123"<<endl;
}  


2.delay()函数
 delay函数要自己写,编译器里没有。
#include <time.h>
void delay(int sec)
{
      time_t start_time, cur_time; // 变量声明
      time(&start_time);
      do {
             time(&cur_time);
     }
      while((cur_time - start_time) < sec );
}


然后就可以直接调用了,如:
#include<iostream.h>
#include <time.h>
void delay(int sec)
{
      time_t start_time, cur_time; // 变量声明
      time(&start_time);

      do {
            time(&cur_time);
     }
      while((cur_time - start_time) < sec );
}


void main()
{
       cout<<"a"<<endl;
       delay(5); // 滞后5秒
       cout<<"b"<<endl;
}


   短于一秒的delay可以这样写:


clock_t start_time, cur_time;
start_time = clock();
while((clock() - start_time) < 3.0 * CLOCKS_PER_SEC)
{
}

posted @ 2012-05-10 16:53  血马雄风  阅读(2729)  评论(0)    收藏  举报