althoug my questionn might be noob-ish, let me explain: I recently started playing with MMF, created 2 processes wich access the same memory Pointer, Process1 writes an integer to MMF, Process2 has a button, which onClick, it displays the first integer in MMF.
What i want to do is, when i "send", write data from Process1 to MMF, Process2 Notices this request ontime, and displays the data on exact time, and so on with new data written.
I'm not sure whether a Thread checking for changes in MMF would be ok, sounds Dirty.
Hope somebody could point me out a solution, because i'm out of ideas :(.
Here's a piece of code:
procedure OpenMap;
var
llInit: Boolean;
lInt: Integer;
begin
if Hmapping<>0 then Exit;
HMapping := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
0, MAPFILESIZE, pchar('wowsniff'));
// Check if already exists
llInit := (GetLastError() <> ERROR_ALREADY_EXISTS);
if (hMapping = 0) then
exit;
PMapData := MapViewOfFile(HMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if PMapData = nil then
exit;
if (llInit) then
begin
// Init block to #0 if newly created
FillChar(PMapData^, MAPFILESIZE, 0);
end
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
LockMap;
PDword(PMapData)^:=Strtoint(edit1.Text);
UnlockMap;
end;
Use a named event object for that, either via
TEvent
orCreateEvent()
. Both processes can create the same event name (just like they are creating the same mapping name), then Process 1 can signal the event whenever it writes new data, and Process 2 can wait for the event to be signaled before reading the data (for real-time reading, you should use a thread for the waiting/reading).You can use a named mutex object, via
TMutex
orCreateMutex()
, to implement your lock/unlock functionality when reading/writing the data.Try something like this:
Process 1 :
Process 2: