I got started using OTL for multithreading, and getting great helps!
A variety of using Parallel.ForEach
have been successful. But now I encountered an unaccountable case.
Please see the simple and full code below:
program test;
{$APPTYPE CONSOLE}
uses
OtlParallel;
var
i: integer;
begin
for i := 1 to 1251 do
Parallel.ForEach(0, 0).Execute(
procedure (const num: integer)
begin
end);
end.
When the iteration number exceeds 1250, an error occurs:
'System Error. Code: 1816. Not enough quota is available to process this command'.
Could I be misunderstanding any basic usage of OTL?
That problem spurs from some (maybe questionable) design solutions inside the OTL and at the moment cannot be resolved (except by processing messages in the main thread, as others have stated).
In any case I would suggest that you further refactor your approach. You could, for example, create a BackgroundWorker abstraction with only one parallel worker and then create 2000 work items and send them to the BackgroundWorker. Inside the background worker's Execute method you can then use Parallel.ForEach to process a task.
Or you could create a BackgroundWorker with multiple parallel tasks (= number of cores) and then run a normal for loop in each task. That would probably give you the fastest performance.
BTW, if you are using Parallel.ForEach in its simple form, then the new Parallel.&For will perform better. It doesn't have all the bells and whistles of ForEach, but is considerably faster.