WM_QUIT的困扰

下面的程序是根据《WIN32多线程程序设计》修改得到的: #include "stdafx.h" #include #include #include #include #include #include #include #include #define MY_MSG WM_USER+100 HANDLE hStartEvent; void CALLBACK TimerFunc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime ) { UNREFERENCED_PARAMETER( hwnd ); UNREFERENCED_PARAMETER( uMsg ); PostThreadMessage( GetCurrentThreadId(), WM_QUIT, 0, 0 ); } unsigned __stdcall fun( void *param ) { printf( "thread fun start/n" ); MSG msg; PeekMessage( &msg, NULL, WM_USER, WM_USER, PM_NOREMOVE ); if( !SetEvent( hStartEvent ) ) { printf( "set start event failed,errno:%d/n", ::GetLastError() ); return 1; } SetTimer( NULL, NULL, 2000, (TIMERPROC)TimerFunc ); bool bRet = false; while ( ( bRet = GetMessage( &msg, 0, 0, 0 ) ) != false ) { if ( bRet == -1 ) { // handle the error and possibly exit } else { // process msg here switch( msg.message ) { case MY_MSG: { char * pInfo = ( char * )msg.wParam; printf( "recv %s/n", pInfo ); delete[] pInfo; } break; default: { if ( msg.message == WM_TIMER ) { printf( "message id = %3d, is WM_TIMER /n", msg.message ); } DispatchMessage( &msg ); } break; } } } if ( msg.message == WM_QUIT ) { printf( "message id = %3d, is WM_QUIT /n", msg.message ); printf( "bRet = %d /n", bRet ); } return 0; } int main() { const int MAX_INFO_SIZE = 20; HANDLE hThread; unsigned nThreadID; hStartEvent = ::CreateEvent( 0, FALSE, FALSE, 0 ); if( hStartEvent == 0 ) { printf( "create start event failed,errno:%d/n", ::GetLastError() ); return 1; } hThread = ( HANDLE )_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID ); if(hThread == 0) { printf( "start thread failed,errno:%d/n", ::GetLastError() ); CloseHandle( hStartEvent ); return 1; } ::WaitForSingleObject( hStartEvent, INFINITE ); CloseHandle( hStartEvent ); int count = 0; char* pInfo = new char[MAX_INFO_SIZE]; sprintf( pInfo, "msg_%d", ++count ); if( !PostThreadMessage( nThreadID, MY_MSG, (WPARAM)pInfo, 0 ) ) { printf( "post message failed,errno:%d/n", ::GetLastError() ); delete[] pInfo; } WaitForSingleObject( hThread, INFINITE ); CloseHandle( hThread ); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值