设置定时器的回调函数没有调用,如何解决?

taixinltd 2004-04-02 08:23:57
// SampClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "winsock2.h"
#include "stdio.h"

char Message[1024] ; // 发送短信
VOID CALLBACK SendTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

WSADATA wsd;
WORD wVersion ;
SOCKET sClient;
struct sockaddr_in ServerAddr,ServerReplyAddr ;
int nRet,nAddrLen ;

int main(int argc, char* argv[])
{
wVersion = MAKEWORD(2,2);
nRet = WSAStartup(wVersion,&wsd);
if ( nRet != 0 )
{
printf("WSAStartup call error : code \n",WSAGetLastError());
return 1;
};
sClient = socket(AF_INET,SOCK_DGRAM,0);
if ( sClient == INVALID_SOCKET)
{
printf("socket() return invalid socket : error code \n",WSAGetLastError());
WSACleanup();
return 2 ;
};

ServerAddr.sin_family = AF_INET ;
ServerAddr.sin_port = htons(12345);
ServerAddr.sin_addr.S_un.S_addr = inet_addr("192.168.0.23") ;

UINT TimerID = SetTimer(NULL,1,1000,(TIMERPROC )SendTimerProc);
if ( TimerID == 0 )
printf("Create Timer Fail \n");
else
printf("create timer succeed : TimerId = %d !\n",TimerID);

while ( true )
{
char c ;
scanf("%c",&c);
};
if ( TimerID != 0 )
KillTimer(NULL,TimerID);
WSACleanup();
return 0;
}
VOID CALLBACK SendTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
printf("begin ...");
sprintf(Message,"wjd");
nAddrLen = sizeof(ServerAddr);
nRet = sendto(sClient,Message,1024,0,( struct sockaddr * ) & ServerAddr,nAddrLen);
if ( nRet == SOCKET_ERROR )
{
printf("call sendto error : code \n",WSAGetLastError());
return ;
}
else
printf("send %d byte message, all Message is %s \n",nRet,Message);



nRet = recvfrom(sClient,Message,1024,0,(struct sockaddr * ) & ServerReplyAddr,&nAddrLen);
if ( nRet == SOCKET_ERROR )
{
printf("recvfrom call error : code %d \n",WSAGetLastError());
return ;
}
else
printf("recvfrom message %s \n",Message);
}

程序运行后,并没有调用SendTimeProc,为什么呢?
...全文
373 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
happlyman 2004-04-02
  • 打赏
  • 举报
回复
哈哈,好了,自己解决了!
taixinltd 2004-04-02
  • 打赏
  • 举报
回复
原来还要加上消息队列处理:
while (GetMessage(&msg, // message structure
NULL, // handle to window to receive the message
NULL, // lowest message to examine
NULL) // highest message to examine
!= 0 && GetMessage(&msg, NULL, NULL, NULL) != -1)
{

// Post WM_TIMER messages to the hwndTimer procedure.

if (msg.message == WM_TIMER)
{
msg.hwnd = hwndTimer;
}

TranslateMessage(&msg); // translates virtual-key codes
DispatchMessage(&msg); // dispatches message to window
}

拷贝别人的,不好意思!

64,962

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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