如题,如何在控制台程序中生成定时器?如果用settimer的话好像有点问题,最后一个参数是回调函数,这个回调函数如何定义?还有没有其他生成定时器的方法,望告知。
谢谢!
谢谢!
解决方案 »
- icmp数据包的判断问题
- ******************请求赠分(标题醒目点)*******************
- 想设计这样的功能:当左键一直按下而不松开时,屏幕上就不停地出现left,left,left.....用什么消息?
- 菜鸟发问:在装过VC++6.0后,没有马上装MSDN,过一段时间之后再装,如何让两者关联起来?谢谢各位!
- 调整窗口大小问题
- 请问在引用dll时,在文件中是否包含lib和dll头文件,两者满足一个就可以了
- 请教一下关于OPENGL的消隐处理问题.急!!!!!!1
- 关于UTF-7解码的问题
- 如何改变将滚动条变为flat样式,如何更改其颜色?
- vs2010创建qt mapnik工程
- vc 6.0 IDE里的字体在哪里能改,就是IDE的文本编辑器里使用的字体。
- 关于UDP服务器处理多用户时转发的问题!!!
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
下面的代码在VC2003中执行,线程会等待10秒
#define _WIN32_WINNT 0x0500#include <windows.h>
#include <conio.h>
#include <iomanip>
#include <iostream>
#include <process.h>
#include <sstream>
using namespace std;
typedef unsigned (__stdcall *PTHREAD_START) (void *);
#define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
pvParam, fdwCreate, pdwThreadId) \
((HANDLE)_beginthreadex( \
(void *) (psa), \
(unsigned) (cbStack), \
(PTHREAD_START) (pfnStartAddr), \
(void *) (pvParam), \
(unsigned) (fdwCreate), \
(unsigned *) (pdwThreadId)))
DWORD WINAPI _ThreadProc(PVOID pvParam);
HANDLE TimeHandle;
void print_now(LPCTSTR msg)
{
std::stringstream ss;
SYSTEMTIME st;
GetLocalTime(&st);
ss<<msg<<" "<<st.wHour<<":"<<st.wMinute<<":"<<st.wSecond<<"."<<st.wMilliseconds<<endl;
cout<<ss.str();
}
int main()
{
SYSTEMTIME stm;
FILETIME ftLocal,ftUTC;
LARGE_INTEGER liUTC;
HANDLE ProcHandle;
DWORD ThreadID;liUTC.QuadPart=-100000000;
TimeHandle = CreateWaitableTimer(NULL,TRUE,NULL);
print_now(__FUNCTION__);
cout<<"等待10秒....."<<endl;cout<<"CreateWaitableTimer error code "<<GetLastError()<<std::endl;
SetWaitableTimer(TimeHandle,&liUTC,0,NULL,NULL,0);
cout<<"SetWaitableTimer error code "<<GetLastError()<<std::endl;ProcHandle=chBEGINTHREADEX(NULL,0,_ThreadProc,NULL,0,&ThreadID);
//CloseHandle(TimeHandle); // this call will activate TimeHandle
CloseHandle(ProcHandle);
cout<<"sleep a minute"<<endl;
print_now(__FUNCTION__);
//Sleep(1*60*1000);
system("pause");}
DWORD WINAPI _ThreadProc(PVOID pvParam)
{
print_now(__FUNCTION__);
DWORD dw_wait = WaitForSingleObject(TimeHandle,INFINITE);
cout<<__FUNCTION__<<" wait return "<<dw_wait<<endl;
cout<<"报时 "<<endl;
print_now(__FUNCTION__);
return 0;
}