Recent

Author Topic: How interact with UI controls in a thread (started with BeginThread)?  (Read 1040 times)

alpine

  • Hero Member
  • *****
  • Posts: 635
Re: How interact with UI controls in a thread (started with BeginThread)?
« Reply #15 on: December 03, 2022, 01:34:20 pm »
I have some advanced experience with multithreading. I believe this is not a specific problem to a single language or framework. It's a problem of multithreading design by nature. Everyone has to be very careful with async processing and bear in mind everytihng should be well guarded if it should be accessed by more than one thread.
That's right.

I don't know how it works behind the scenes of RemoveAsyncCalls, I'd rather not using it because it may abruptly kill any ongoing async work which might leave some conditions corrupt (e.g., event signal'ed but not unset). In the most simple and naive case, I prefer setting up a boolean variable to indicate if it is valid to perform any work in an async procedure.
It works pretty straightforward. Application.QueueAsyncCall() places a TDataEvent into a queue to be executed into the TApplication.ProcessMessages, i.e. into the UI thread where it is safe to update the UI. Application.RemoveAsyncCalls() removes from the queue the entries for a given instance. That is pretty much all.

But being async in an non refcounted language as FPC means that you should be a bit more cautious.
I was confused when my program started to trigger GPF's at exit and it was only occasionally.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1171
    • Lebeau Software
Re: How interact with UI controls in a thread (started with BeginThread)?
« Reply #16 on: December 04, 2022, 03:10:30 am »
I assumed that by declaring a class/static procedure, it should match BeginThread signature, but apprently I am wrong. What's wrong with my syntax?

A class method, and a static method, are two different things.

A class method still has an implicit Self parameter, which points at a class type rather than an object instance.  This is not compatible with BeginThread().

A static method does not have a Self parameter at all.  This is compatible with BeginThread().

A static method must also be a class method, but a class method does not have to be a static method.

Try this:

Code: Pascal  [Select][+][-]
  1. class function FindInfFile(p: pointer): PtrInt; static;
  2. m_workerID := BeginThread(@TMainForm.FindInfFile, Self);
          
BTW, I know that I can declare a top level bare procedure for this, but I am just curious why a class procedure won't do the work.

It should, once you add static to its declaration.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

cdbc

  • Sr. Member
  • ****
  • Posts: 414
    • http://www.cdbc.dk
Re: How interact with UI controls in a thread (started with BeginThread)?
« Reply #17 on: December 04, 2022, 12:21:38 pm »
Hi
I just use 2 threadsafe Queues ~ data in/out, messages for notification, and poll the queues. It works like a charm... Indeed with beginThread.
Regards Benny
If it ain't broke, don't fix it ;)

 

TinyPortal © 2005-2018