0

I am using Delphi xe 5 for an application which uses Indy to manage my server application

Each client connection gets its own session which is perfect

Now I need to perform several tasks in aysnc way within one of these sessions

When all of these tasks have been completed control can go back to the caller

I am looking into using a local scoped thread pool in my procedure which will place each task to be performed into the thread pool

I have seen the TIdThreadMgrPool which looks perfect

How do wait until all of my threads have finished?

Paul

Paul
  • 2,773
  • 7
  • 41
  • 96

1 Answers1

1

Indy 9's TIdThreadMgrPool component (replaced with the TIdSchedulerOfThreadPool component in Indy 10) is not a general-purpose thread pool. It is designed for use only as the client thread pool for Indy's TIdTCPServer component. There is no option to wait for active threads to finish running, TIdTCPServer handles that internally while it is being shut down.

There are plenty of 3rd party general-purpose thread-pool implementations available for Delphi, such as OmniThreadLibrary. If you were using XE7, you could take advantage of its new Parallel Programming Library instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks I cant use XE 7 I was going to use OmniThreadLibrary but I am getting problems catching the cause of an exception and I am worried about whether it is a good idea to combine this with Indy because the OmniThreadLibrary uses a global thread pool so I am worried about other indy sessions being slowed down by the use of a global pool, hence looking for something where I can run tasks async with the context of 1 procedure – Paul Apr 06 '15 at 17:03
  • If you just want a local pool that blocks the calling thread until the tasks are finished, you could just create an array of normal `TThread` objects and pass their `Handle` values to the Win32 API `WaitForMultipleObjects()` function. – Remy Lebeau Apr 06 '15 at 17:47
  • Thanks! Is the approach here a good one? http://stackoverflow.com/questions/6867105/waiting-for-multiples-threads-using-waitformultipleobjects See Sertac Akyuz's answer – Paul Apr 06 '15 at 18:47
  • Yes, that is basically what I was thinking, just without the `repeat` loop and setting the `bWait` parameter to `True` instead of `False`. A `bWait=False` loop is useful when you have other things to do while waiting, but if you don't then the loop is overkill. – Remy Lebeau Apr 06 '15 at 19:39
  • I dont have anything else to do in this procedure but if I set that to true would it lock up by whole application? Am not in the office at the moment so I cant check. This is part of an Indy Server application so whilst I want the executing session to wait, I dont want the whole application to wait – Paul Apr 06 '15 at 20:54
  • Using `bWait=True` would only block the calling thread, and only until all of the task threads have finished. Your whole app would not lock up, unless it does something that needs to wait on the blocked thread (like shutting down the server while tasks are running). – Remy Lebeau Apr 06 '15 at 21:44
  • ok thanks I will mark your comment as a valid answer. I connected to the office and the approach itself works I am having major problems at the moment with the thread but I have posted a new question for that http://stackoverflow.com/questions/29480402/how-can-i-use-a-dictionary-stringlist-inside-execute-of-a-thread-delphi – Paul Apr 06 '15 at 22:02