作者在 2008-11-25 16:48:20 发布以下内容
#define EVENT_NAME "EVENT_NAME"
HANDLE g_Event = NULL;
#define WM_hThreaEnd (WM_USER+101)
extern CPraDesktopApp theApp;
HANDLE g_Event = NULL;
#define WM_hThreaEnd (WM_USER+101)
extern CPraDesktopApp theApp;
创建信号:
g_Event = CreateEvent(NULL, FALSE, FALSE, EVENT_NAME);
发送信号:
_SetEvent(EVENT_NAME);
BOOL WINAPI _SetEvent(char* pszEventName)
{
if (!pszEventName) return FALSE;
char szEventName[MAX_PATH] = {0};
memcpy(szEventName,pszEventName,MAX_PATH);
strupr(szEventName);
HANDLE hEvent;
hEvent= OpenEvent(EVENT_MODIFY_STATE,FALSE,szEventName);
if (!hEvent) return FALSE;
SetEvent(hEvent);
CloseHandle(hEvent);
return TRUE;
}
{
if (!pszEventName) return FALSE;
char szEventName[MAX_PATH] = {0};
memcpy(szEventName,pszEventName,MAX_PATH);
strupr(szEventName);
HANDLE hEvent;
hEvent= OpenEvent(EVENT_MODIFY_STATE,FALSE,szEventName);
if (!hEvent) return FALSE;
SetEvent(hEvent);
CloseHandle(hEvent);
return TRUE;
}
接收信号:
unsigned int __stdcall MsgProc(LPVOID lpParameter)
{
while (1)
{
if(WaitForSingleObject(g_Event,50) == WAIT_OBJECT_0)
{
PostMessage(theApp.m_pMainWnd->m_hWnd,WM_hThreaEnd,0,0);
}
{
while (1)
{
if(WaitForSingleObject(g_Event,50) == WAIT_OBJECT_0)
{
PostMessage(theApp.m_pMainWnd->m_hWnd,WM_hThreaEnd,0,0);
}
}
return true;
}
开信号监听线程:
UINT m_hUIThread;
UINT dwThreadID = 0;
m_hUIThread = _beginthreadex(NULL,0,MsgProc,&arg,0,&dwThreadID);
::CloseHandle((HANDLE)m_hUIThread);
if(m_hUIThread == -1)
{
::MessageBox(NULL,"_beginthreadex error",NULL,MB_OK);
return;
}