-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Too many Threads Solution #2
Comments
Interesting idea. It does dumb things down somewhat by using a single message type. My general advice when using this library in a UI is to limit the number of UI dispatchers as they would call sychronise too often otherwise. When time permits I will see if I can come up with a better solution. |
Yeah, it was a quick fix and something more elegant could probably be done – such as registering which message types you wanted to receive so it could more intelligently filter/direct them…
Or perhaps a better way of doing it would be to define a base MessageObject which could be subclassed to add specific data, something like
type
TReceiveMessageEvent = function (const MessageID:TMessageID; MessageData:TMessageObject; FreeOnRead:Boolean=False):integer of object;
TMessageObject = class(TObject)
StringData:String;
IntegerData:Int64;
FloatData:Double;
End;
TMessageReceiver = class(TComponent)
Private
fOnReceivedMessage:TReceiveMessageEvent;
Function InternalReceiveMessage(const MessageID:TMessageID; MessageData:TMessageObject; FreeOnRead:Boolean=False):integer;
public
Procedure Loaded; override;
Property OnReceivedMessage: TReceiveMessageEvent read fOnReceivedMessage write fOnReceivedMessage;
End;
RegisterWithMessageServer(ComponentToBeNotified:TComponent;ReceivedEvent:TReceivedMessageEvent;MessagesToReceive:Array of TMessageID);
Procedure TMessageReceiver.Loaded;
Begin
***@***.***,[wm_mymessage1,wm_mymessage2]);
End;
Function TMessageReceiver.InternalReceiveMessage(const MessageID:TMessageID; MessageData:TMessageObject; FreeOnRead:Boolean=False):integer;
Begin
If Assigned(fOnReceivedMessage) then
Result:=(fOnReceivedMessage(MessageID,MessageData)
If Result=S_OK) and FreeOnread and Assigned(messageData) then
FreeANdNil(MessageData);
End;
From: Vincent Parrett ***@***.***>
Sent: Wednesday, May 25, 2022 6:10 PM
To: VSoftTechnologies/VSoft.Messaging ***@***.***>
Cc: TheOriginalBytePlayer ***@***.***>; Author ***@***.***>
Subject: Re: [VSoftTechnologies/VSoft.Messaging] Too many Threads Solution (Issue #2)
Interesting idea. It does dumb things down somewhat by using a single message type.
My general advice when using this library in a UI is to limit the number of UI dispatchers as they would call sychronise too often otherwise.
When time permits I will see if I can come up with a better solution.
—
Reply to this email directly, view it on GitHub <#2 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AEJYX4HRTBMUPEWSZNL2TVTVL2QLNANCNFSM5W4QXXRA> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/AEJYX4GO6AABGV3ELW5SWUDVL2QLNA5CNFSM5W4QXXRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOIPJNXSQ.gif> Message ID: ***@***.*** ***@***.***> >
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was using your great component only to discover that it was creating a huge number of threads (200+) in our application which made debugging it basically impossible under Win64. I put together a simple -- read perhaps kludged solution -- that brings it down to 2 threads which I'm sure you could make more elegant and generic, if you desired.
Basically the way I did it was to create a centralized collector of components that want to receive messages, one which receives all the messages sent using an internal messaging channel, then feeds them back out to the registered components through an exterior messaging channel. This brings it down from one thread per IMessagingDispatcher to only two threads regardless as to how many components are registered.
I did some timing on it and 1600 calls took 6ms so I didn't bother trying to do any additional optimization though I'm sure it could be done.
There are two parts to this, the first is a component you can just drop on a form and it will automatically register the form with the messaging system.
The second is a SendMessage function that you can use with a generic messaging structure to pass most data.
The text was updated successfully, but these errors were encountered: