Using events in threads between processes - C
Posted
by Jamie Keeling
on Stack Overflow
See other posts from Stack Overflow
or by Jamie Keeling
Published on 2010-04-22T13:43:11Z
Indexed on
2010/04/23
17:23 UTC
Read the original article
Hit count: 257
Hello all!
I have an application consisting of two windows, one communicates to the other and sends it a struct constaining two integers (In this case two rolls of a dice).
I will be using events for the following circumstances:
- Process a sends data to process b, process b displays data
- Process a closes, in turn closing process b
- Process b closes a, in turn closing process a
I have noticed that if the second process is constantly waiting for the first process to send data then the program will be just sat waiting, which is where the idea of implementing threads on each process occurred and I have started to implement this already.
The problem i'm having is that I don't exactly have a lot of experience with threads and events so I'm not sure of the best way to actually implement what I want to do.
Following is a small snippet of what I have so far in the producer application;
Create thread:
case IDM_FILE_ROLLDICE:
{
hDiceRoll = CreateThread(
NULL, // lpThreadAttributes (default)
0, // dwStackSize (default)
ThreadFunc(hMainWindow), // lpStartAddress
NULL, // lpParameter
0, // dwCreationFlags
&hDiceID // lpThreadId (returned by function)
);
}
break;
The data being sent to the other process:
DWORD WINAPI ThreadFunc(LPVOID passedHandle)
{
HANDLE hMainHandle = *((HANDLE*)passedHandle);
WCHAR buffer[256];
LPCTSTR pBuf;
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
struct diceData storage;
HANDLE hMapFile;
DWORD dw;
//Roll dice and store results in variable
storage = RollDice();
hMapFile = CreateFileMapping(
(HANDLE)0xFFFFFFFF, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
dw = GetLastError();
MessageBox(hMainHandle,L"Could not create file mapping object",L"Error",MB_OK);
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
MessageBox(hMainHandle,L"Could not map view of file",L"Error",MB_OK);
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, &storage, (_tcslen(szMsg) * sizeof(TCHAR)));
//_getch();
MessageBox(hMainHandle,L"Completed!",L"Success",MB_OK);
UnmapViewOfFile(pBuf);
return 0;
}
I'm trying to find out how I would integrate an event with the threaded code to signify to the other process that something has happened, I've seen an MSDN article on using events but it's just confused me if anything, I'm coming up on empty whilst searching on the internet too.
Thanks for any help
Edit:
I can only use the Create/Set/Open methods for events, sorry for not mentioning it earlier.
© Stack Overflow or respective owner