Leo*_*Leo 8 delphi multithreading synchronize
It makes me confused when I read the article by Zarko Gajic today:
"Multithreaded Delphi Database Queries"
Article URL: http://delphi.about.com/od/kbthread/a/query_threading.htm
Sourecode: http://delphi.about.com/library/weekly/code/adothreading.zip
With the code of "TCalcThread.Execute" procedure, Why the following code do not need to be placed in the Synchronize() method to run?
Line 173: ListBox.Clear;
Line 179: ListBox.Items.Insert(......);
Line 188: ListBox.Items.Add('*---------*');
Line 195: TicksLabel.Caption := 'Ticks: ' + IntToStr(ticks);
Run Code Online (Sandbox Code Playgroud)
These codes are operating the VCL components, and are related to the UI updates. In my knowledge, these operations should be use thread synchronize, and executed by the main thread. Is my knowledge has the flaw?
All*_*uer 18
这是一种罕见的情况,您可以从Windows为您执行线程同步这一事实中受益.原因是对于列表框,使用带有控件特定消息的SendMessage操作项目.因此,每个SendMessage调用都会确保消息由创建控件的同一线程处理,特别是主线程.
就像我说的,这是一种罕见的情况.它还会导致这三个调用中的每一个都进行线程切换,这会降低性能.你最好还是使用Synchronize来强制该代码块在它所属的主线程中运行.它还确保如果您开始使用不在内部使用SendMessage的控件,您将不会被咬.