SetTimer不生效,回调函数不执行

jufeng2309 2014-02-25 02:52:14
主函数

int main()
{
int iRet = 0;
DWORD dwThreadId;
HANDLE hThread;
DWORD exitCode = 0;

hThread = CreateThread(NULL,0,DoThread,0,0,&dwThreadId);
PRINT("线程已创建成功...");
dwStat = WaitForSingleObject(hThread,INFINITE);
CloseHandle(hThread);
return 0;
}


定时执行函数

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
PRINT("Hello......");
}



线程函数

DWORD CALLBACK DoThread(PVOID pvoid)
{
MSG msg;
UINT timerid;
BOOL bRet;
char sCurDateTime[18] = {0};


PRINT("进入线程处理...");

bRet = PeekMessage(&msg,NULL,WM_USER,WM_USER, PM_NOREMOVE);

timerid=SetTimer(NULL,123,2000,TimerProc);

while( (bRet = GetMessage(&msg,NULL,0,0))!= 0)
{
if(bRet==-1)
{
// handle the error and possibly exit
PRINT("GetMessage Error...");
}
else if (msg.message = WM_USER)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
PRINT("接收到WM_USER消息....");
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

KillTimer(NULL,timerid);

PRINT("线程处理结束...");
return 0;
}



现在有两个疑问
1、为什么SetTimer的回调函数不行
2、SetTimer执行的时候会往线程消息队列里发WM_USER消息吗?如果不是
WM_USER消息是在什么时候往线程队列放的?

谢谢大家。。。比较着急。。。

...全文
931 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
丹锋无量 2014-02-26
  • 打赏
  • 举报
回复
貌似楼主已经建立起了自己的消息循环,但为什么回调函数未启动确实有些奇怪,楼主需要调试一下,看看你的消息循环里收到WM_TIMER消息没有。
Eleven 2014-02-26
  • 打赏
  • 举报
回复

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
	printf("Hello......\r\n");
}

DWORD CALLBACK DoThread(PVOID pvoid) 
{   
	MSG  msg;
	UINT timerid;
	BOOL  bRet;
	char sCurDateTime[18] = {0};


	printf("进入线程处理...\r\n");

	//bRet = PeekMessage(&msg,NULL,WM_USER,WM_USER, PM_NOREMOVE);

	timerid = SetTimer(NULL, 123, 2000, TimerProc);   

	while(GetMessage(&msg, NULL, 0, 0))
	{     
		TranslateMessage(&msg);     
		DispatchMessage(&msg);     
	}   

	KillTimer(NULL, timerid);   

	printf("线程处理结束...\r\n");   
	return 0;   
}

int main()
{
	int iRet = 0;
	DWORD   dwThreadId; 
	HANDLE  hThread;
	DWORD   exitCode = 0;

	hThread = CreateThread(NULL,0,DoThread,0,0,&dwThreadId);
	
	printf("线程已创建成功...\r\n");
	
	WaitForSingleObject(hThread,INFINITE);
	CloseHandle(hThread);
	return 0;
}
Cherishe7 2014-02-26
  • 打赏
  • 举报
回复
在线程中为什么要settimer,没有创建WM_TIMER消息
许文君 2014-02-26
  • 打赏
  • 举报
回复
同上,消息循环有误
「已注销」 2014-02-26
  • 打赏
  • 举报
回复
你那个接收消息的代码能成功吗?
while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
    if(bRet == -1)
    {
        // handle the error and possibly exit
        PRINT("GetMessage Error...");
    }
    else if (msg.message = WM_USER)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        PRINT("接收到WM_USER消息....");
    }
    else
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
我怎么看你这段根本就没有循环直接出错退出了。
lijianli9 2014-02-26
  • 打赏
  • 举报
回复
控制台程序需要手工添加 getmessage,tranlmessage等消息函数来手工建立消息机制,因为settimer需要消息机制支持。
Eleven 2014-02-26
  • 打赏
  • 举报
回复
引用 10 楼 jufeng2309 的回复:
WM_USER消息是怎么产生的?在消息循环体里面,貌似又接收到到WM_USER消息?
应该和你调用SetTimer(NULL, ...);有关系~
jufeng2309 2014-02-26
  • 打赏
  • 举报
回复
WM_USER消息是怎么产生的?在消息循环体里面,貌似又接收到到WM_USER消息?
oyljerry 2014-02-25
  • 打赏
  • 举报
回复
Timer是WM_TIMER消息 你需要创建消息循环,这样才能有消息队列,接受消息
worldy 2014-02-25
  • 打赏
  • 举报
回复
引用 楼主 jufeng2309 的回复:
主函数

int main()
{
	int iRet = 0;
	DWORD   dwThreadId; 
	HANDLE  hThread;
	DWORD   exitCode = 0;

	hThread = CreateThread(NULL,0,DoThread,0,0,&dwThreadId);
	PRINT("线程已创建成功...");
	dwStat = WaitForSingleObject(hThread,INFINITE);
	CloseHandle(hThread);
        return 0;
}
定时执行函数

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
       PRINT("Hello......");
}
线程函数

DWORD CALLBACK DoThread(PVOID pvoid) 
{   
        MSG  msg;
	UINT timerid;
	BOOL  bRet;
	char sCurDateTime[18] = {0};


	PRINT("进入线程处理...");

	bRet = PeekMessage(&msg,NULL,WM_USER,WM_USER, PM_NOREMOVE);

        timerid=SetTimer(NULL,123,2000,TimerProc);   

        while( (bRet = GetMessage(&msg,NULL,0,0))!= 0)
	{     
        if(bRet==-1)   
	{   
            //   handle   the   error   and   possibly   exit
			PRINT("GetMessage Error...");
	}
	else if (msg.message = WM_USER)
	{
	    TranslateMessage(&msg);     
            DispatchMessage(&msg);
	    PRINT("接收到WM_USER消息....");
	}
        else
        {    
            TranslateMessage(&msg);     
            DispatchMessage(&msg);     
        }  
	}   
    
	KillTimer(NULL,timerid);   
    
	PRINT("线程处理结束...");   
    return 0;   
}
现在有两个疑问 1、为什么SetTimer的回调函数不行 2、SetTimer执行的时候会往线程消息队列里发WM_USER消息吗?如果不是 WM_USER消息是在什么时候往线程队列放的? 谢谢大家。。。比较着急。。。
控制台的程序,哪来的消息?
孤客天涯 2014-02-25
  • 打赏
  • 举报
回复
1.SetTimer是需要消息队列支持的,你的程序没有窗口,也就没有消息机制 2.不明白LZ为什么要在线程中SetTimer?,有什么目的?如果只是实现定时器功能,只直用线程就可以模拟出来 DWORD CALLBACK DoThread(PVOID pvoid) { while(退出条件) { Sleep(xxx); DoSomething(); } 3.若要创建无窗口的定时器,可以用多媒体定时器TimeSetEvent... {

15,471

社区成员

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

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