[Lazarus] Enqueuing a callback from a thread via other class - or am I overcomplicating this ?
Lukasz Sokol
el.es.cr at gmail.com
Tue Jun 20 10:29:46 CEST 2017
On 19/06/17 10:05, Lukasz Sokol via Lazarus wrote:
> On 19/06/17 00:30, José Mejuto via Lazarus wrote:
>> El 18/06/2017 a las 22:44, el es via Lazarus escribió:
>>
>>> Hence the object, would have to have ITS callback routine be something like a TNotifyEvent ( procedure(AObject: TSomeObject) of object, or something), and the flag set is set when the callback returns to the object context like
>>>
>>> procedure TSomeObject.Callback;
>>> // this procedure is enQueue()d by the thread
>>> begin
>>> if Assigned({Self.}FSomeObjectNotifyEvent) then
>>> FSomeObjectNotifyEvent(Self);
>>
>> Hello,
>>
>> Just exit the procedure and there is not free problem. You will get an exception when you use object data, not object code.
>>
>
> I did now, and also, beforehands I did not have the TSomeObjectNotifyEvent() type of callback...
> so in the callback I had to browse through the list of TSomeObject's, searching for the right one - and that was really risky;
>
>
>
>>
>> procedure TSomeObject.Callback;
>> // this procedure is enQueue()d by the thread
>> begin
>> if Assigned({Self.}FSomeObjectNotifyEvent) then
>> FSomeObjectNotifyEvent(Self);
>> Self.CanBeDestroyed := true;
>> Exit;
>> end;
>>
>> After you set CanBeDestroyed to true you must consider all data as RANDOM, so don't access any property or variable.
>>
> Understood, I ensured there is nothing accessing the TSomeObject sent as Self to the event handler, after it comes back.
>
I also had dual-usage of the TSomeObjects: sometimes, they were supposed to be 'just' processed by the thread and then discarded,
some were supposed to be with callback to the main thread;
But I was still hitting the F0F0F0F0 when the above was being queued, when FSomeObjectNotifyEvent was nil;
So actually, I had to move the (equivalent of)
SomeObject.CanBeDestroyed := true;
to be the very last call of the FSomeObjectNotifyEvent(Self) handler, in the main thread,
to handle the ones with callback (enclosed in finally...end),
and in the worker thread, to only Queue() the callbacks if the handler is subscribed to:
if Assigned(SomeObject.FSomeObjectNotifyEvent) then
Queue(SomeObject.FSomeObjectNotifyEvent);
> Thanks,
>
> -L.
More information about the Lazarus
mailing list