线程中定时器问题

jalloy14 2007-11-07 02:38:58
请教:
我要完成一个操作,每隔一段时间要检测接口状态,我新开了一个线程做这个工作,在这个线程中设了个定时器,每隔10秒去检测接口状态,具体代码如下(如下代码是在新开的线程中执行):
HANDLE m_hTimeHandle = NULL;
LARGE_INTEGER liDueTime;
BOOL bResult;

//定时器定10秒
liDueTime.QuadPart = -100000000;
//创建waitable timer
m_hTimeHandle = CreateWaitableTimer(NULL, TRUE, NULL);

//设置定时时间
bResult = SetWaitableTimer(m_hTimeHandle, &liDueTime, 0, NULL, NULL, 0);

//等待对象产生信号
DWORD dwState = WaitForSingleObject(m_hTimeHandle, INFINITE);
if (dwState != WAIT_OBJECT_0)
{
return 0;
}
else
{
检测接口状态过程..
}
在运行中,发现该定时器在线程中只运行一次,请各位指点,难道是运行一次线程就结束了?应该怎么解决,在线等,谢谢。
...全文
1075 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
WooSir 2007-11-15
  • 打赏
  • 举报
回复
我用过类似的方法,不过线程中还用了循环
HANDLE g_hWaitableTimer10 = NULL;//高精度的10s定时器
bool bExit=false;

VOID CALLBACK TimerProc10(LPVOID p, DWORD dwTimerLowValue, DWORD dwTimerHighValue){};//以10s为单位进行线程函数循环

UINT ThreadFun( LPVOID pParam )
{
g_hWaitableTimer10 = CreateWaitableTimer(NULL, TRUE, NULL);
if (g_hWaitableTimer10==NULL)
{
AfxMessageBox( _T("内核定时器创建失败!\n"), NULL, MB_ICONSTOP);
AfxEndThread(20);
}
LARGE_INTEGER lli = {0};
if (SetWaitableTimer(g_hWaitableTimer10, &lli, 10000, TimerProc20, NULL, false))//高精度的10000ms定时器
while (!bExit)
{
检测接口状态过程......
SleepEx(100000, true);//这个100s睡眠会在每隔10000ms后被唤醒
}
}

这样就可以每隔10秒进行接口状态检测了
CherishLemon 2007-11-14
  • 打赏
  • 举报
回复
等写成死循环
但要找个出口
while(1)
{
HANDLE m_hTimeHandle = NULL;
LARGE_INTEGER liDueTime;
BOOL bResult;

//定时器定10秒
liDueTime.QuadPart = -100000000;
//创建waitable timer
m_hTimeHandle = CreateWaitableTimer(NULL, TRUE, NULL);

//设置定时时间
bResult = SetWaitableTimer(m_hTimeHandle, &liDueTime, 0, NULL, NULL, 0);

//等待对象产生信号
DWORD dwState = WaitForSingleObject(m_hTimeHandle, INFINITE);
if (dwState != WAIT_OBJECT_0)
{
continue;//或者提示一下AfxMessage
}
else
{
检测接口状态过程..
}

}
coldplay968 2007-11-13
  • 打赏
  • 举报
回复
好像你的线程 执行一次就自动退出了
jpinglove 2007-11-08
  • 打赏
  • 举报
回复
学习下
yxz_lp 2007-11-07
  • 打赏
  • 举报
回复

#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>

HANDLE gDoneEvent;

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
if (lpParam == NULL)
{
printf("TimerRoutine lpParam is NULL\n");
}
else
{
// lpParam points to the argument; in this case it is an int

printf("Timer routine called. Parameter is %d.\n",
*(int*)lpParam);
}

SetEvent(gDoneEvent);
}

int main()
{
HANDLE hTimer = NULL;
HANDLE hTimerQueue = NULL;
int arg = 123;

// Use an event object to track the TimerRoutine execution
gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (NULL == gDoneEvent)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return 1;
}

// Create the timer queue.
hTimerQueue = CreateTimerQueue();
if (NULL == hTimerQueue)
{
printf("CreateTimerQueue failed (%d)\n", GetLastError());
return 2;
}

// Set a timer to call the timer routine in 10 seconds.
if (!CreateTimerQueueTimer( &hTimer, hTimerQueue,
(WAITORTIMERCALLBACK)TimerRoutine, &arg , 10000,/*设为10000每10秒调用一次*/ 0, 0))//这样就调一次用
{
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
return 3;
}

// TODO: Do other useful work here

printf("Call timer routine in 10 seconds...\n");

// Wait for the timer-queue thread to complete using an event
// object. The thread will signal the event at that time.

if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());

// Delete all timers in the timer queue.
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());

return 0;
}
这是msdn上的例子,你可以把检测接口状态代码写在TimerRoutine函数里,然后定时调用。
yriio 2007-11-07
  • 打赏
  • 举报
回复
循环都么 自然是一次线程就结束了..
LanglyTiger 2007-11-07
  • 打赏
  • 举报
回复
不能直接sleep?
stivenjia 2007-11-07
  • 打赏
  • 举报
回复
不知道你怎么做的
我先说以下线程的执行流程。
当你的事件触发后你的程序的到执行然后开始执行,当执行到return后你的线程就over了也就说你的线程结束了。
因此只执行了一次
jalloy14 2007-11-07
  • 打赏
  • 举报
回复
yxz_lp,能否具体写个例子
yxz_lp 2007-11-07
  • 打赏
  • 举报
回复
用CreateTimerQueueTimer(),具体用法查MSDN

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧