I have a program that uses Thread that does some work. The thread should notify another thread (in this example, the main thread) of progress.
If I use Synchronize () to do the synchronization, everything works as expected. If I synchronize with the main thread and publish for-variable and put it on the list, each value will be correctly printed in my ListBox:
procedure TWorkerThread.Execute; var i: Integer; begin inherited; for i := 1 to 1000 do begin Synchronize( procedure() begin FireEvent(i); end); end; end;
Output: 1, 2, 3, 4, 5 ... 1000
If I use Queue () to do the synchronization, the output is not as expected:
procedure TWorkerThread.Execute; var i: Integer; begin inherited; for i := 1 to 1000 do begin Queue( procedure() begin FireEvent(i); end); end; end;
Output: 200, 339, 562, 934, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, [...]
What's going on here? As far as I understand, an anonymous procedure should fix the variable "i"?