When each instance of my program does a database operation, it broadcasts a message across the network with the following information:
- Table Name (actually ID)
- Operation which can be either Insert, Update, or Delete
- Record ID
When an instance receives this kind of message (even the message owner) it broadcasts it as a Windows message to all the open forms. Eventually, each form processes the Windows message to update its current state.
As I already said, I use MultiCast protocol (Indy implementation) to broadcast the message. But notice that MultiCast does not work on a machine without network connection (a socket error occurs).
Here is the actual code in my application:
- Code: Select all
// broadcast the message to the application's forms
procedure TDM.LocalBroadcast(const Message: TMessage);
var
I: Integer;
begin
for I := 0 to Screen.FormCount - 1 do
with Screen.Forms[I] do
if HandleAllocated then
PostMessage(Handle, Message.Msg, Message.WParam, Message.LParam);
end;
// broadcasts the message accross the network
procedure TDM.NetworkBroadcast(const Message: TMessage);
begin
if MultiCastServer.Active then { are we connected to a network? }
MultiCastServer.SendBuffer(Message, SizeOf(Message))
else
LocalBroadcast(Message);
end;
// dispatch meesages received from network to the application's forms
procedure TDM.MultiCastClientIPMCastRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Message: TMessage;
begin
AData.Position := 0;
while AData.Read(Message, SizeOf(Message)) = SizeOf(Message) do
LocalBroadcast(Message);
end;