HostNamedEventObject 进程
#include "stdafx.h"
#include <afxwin.h>
#include <windows.h>
HANDLE hEvent = NULL;
char g_cArray[10];
UINT ThreadProcFirst(LPVOID pParam);
int main(int argc, char* argv[])
{
/*创建一个可以自动复位的事件内核对象*/
hEvent = CreateEvent(NULL, FALSE, FALSE, "Global\\NamedEvent");
/*hEvent = CreateEvent(NULL, FALSE, FALSE, "Local\\NamedEvent");*/
/*置位*/
SetEvent(hEvent);
AfxBeginThread(ThreadProcFirst, NULL);
/*先启动Host, 再启动Target*/
Sleep(5*1000);
CString strResult = CString(g_cArray);
AfxMessageBox(strResult);
CloseHandle(hEvent);
return 0;
}
UINT ThreadProcFirst(LPVOID pParam)
{
/*无限等待hEvent的复位*/
WaitForSingleObject(hEvent, INFINITE);
printf("First");
for (int i = 0; i < 10; ++i)
{
g_cArray[i] = 'a';
Sleep(1);
}
SetEvent(hEvent);
return 0;
}
|
TargetNamedEventObject 进程
#include "stdafx.h"
#include <afxwin.h>
#include <windows.h>
char g_cArray[10];
int main(int argc, char* argv[])
{
HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Global\\NamedEvent");
/*HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Local\\NamedEvent");*/
if (NULL == hEvent) return -1;
WaitForSingleObject(hEvent, INFINITE);
printf("Second");
for (int i = 0; i < 10; ++i)
{
g_cArray[10-i-1] = 'b';
Sleep(1);
}
SetEvent(hEvent);
CString strResult = CString(g_cArray);
AfxMessageBox(strResult);
return 0;
}
|
阅读(556) | 评论(0) | 转发(0) |