4

I have only limted experience with threads. I want to read some fundamental database tables in parallel and I need to wait until all tables are read before the program can reasonably progress. In that regard, blocking the main thread is OK for me.

This code (simplified) works fine:

procedure ReadDBMultiThread;
  var ATasks : Array of ITask;
begin
  SetLength(ATasks, 3);
  ATasks[0] := TTaskCreate(procedure() begin DB_ReadTable1; end);
  ATasks[1] := TTaskCreate(procedure() begin DB_ReadTable2; end);
  ATasks[2] := TTaskCreate(procedure() begin DB_ReadTable3; end);

  ATasks[0].Start;
  ATasks[1].Start;
  ATasks[2].Start; 

  TTask.WaitForAll(ATasks);
end;

However, let's say I would like to update the main form to show the progress, i.e. which database table has already been read (or do any other necessary main thread work). Obviously, I cannot use Synchronise() because that would lead to a dead lock with WaitForall() and I cannot use Queue() because that would by executed after WaitForAll() finishes.

So, is there a good solution to solve this "WaitForAll vs Synchronise" situation? I guess, this must be a situation many people come into... the need to wait for all being finished, but wanting to update the main thread...

I thought about something like this, given in pseudo-code, replacing the WaitForAll() statement:

repeat 
  Applicaton.ProcessMessages; // or "ProcessSynchroniseMessages"
until "AllTaskCompleted"(ATasks);

Would this work? Is there a better solution?

Could I write a own routine like ProcessMessages, but restricted to Synchronise messages, i.e. other main form events not being executed until later?

Many thanks in advance!

8
  • Don't block the main thread. That's your real problem. Pointless to update UI if you aren't pumping the queue. Use an event to signal when you are done. Commented Aug 13, 2015 at 22:34
  • 1
    @DavidHeffernan: "blocking the main thread is OK for me" Commented Aug 13, 2015 at 22:36
  • @DavidHefferman: What do you suggest to do in the main form while waiting for absolutely necessary data? I cannot think of much more than showing an HourGlass-cursor and waiting for the data arrive...
    – HJay
    Commented Aug 13, 2015 at 23:18
  • 1
    @HJay: maybe staying responsive to UI paints, at least. Commented Aug 14, 2015 at 0:53
  • 1
    @DavidHeffernan: Naturally, you are right to teach best practice and as you see, I explicitly asked you how you would solve situations where you have to wait for data. Of course I am interested in doing it right. I believe, Remy Lebeau found are very good way to answer my question while even providing a better solution than I asked for. I hope you are satisfied with that solution, too, as it updates the GUI and waits for my data being there.
    – HJay
    Commented Aug 14, 2015 at 8:28

1 Answer 1

8

TThread.Synchronize() and TThread.Queue() do not use window messages (well, there is a message to "wake up" the main thread to signal a pending request, but the actual synchronizing itself is not message-based). Requests are put into a global queue, which the main thread checks when it is sitting idle, or when it detects the "wake up" message. You can pump that same queue manually by calling the Classes.CheckSynchronize() function directly:

while not TTask.WaitForAll(ATasks, 1000) do
begin
  // process any pending TThread.Synchronize() and TThread.Queue() requests
  CheckSynchronize(0);
  // process any pending UI paint requests, but not other messages
  Application.MainForm.Update;
  // anything else you need...
end;
4
  • Thanks, that looks exactly like what I was asking for... it's 1:20 am now, though, so I postpone the evaluation until tomorrow...
    – HJay
    Commented Aug 13, 2015 at 23:20
  • @Remy WM_PAINT messages will only be synthesised when the queue has been emptied. So process any pending UI paint requests, but not other messages can't be done. Commented Aug 14, 2015 at 13:21
  • @DavidHeffernan: Yes, it can. What you said is true for WM_PAINT messages that are retrieved from the message queue, but there are other ways for a window to receive a WM_PAINT message. Case in point, TWinControl.Update() calls the Win32 UpdateWindow() to force an immediate WM_PAINT message that bypasses the message queue if any portion of the control has been invalidated waiting for painting. Commented Aug 14, 2015 at 15:14
  • Well, better hope that all the paint messages come that way Commented Aug 14, 2015 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.