Recently in writing a program to use more than a thread, so the CB under the multithreading have a certain degree of learning.
Now tell me some of your own experiences. The level is limited, write very rough, please forgive me.
CB relative to VC, under the CB to write multithreaded routines is very simple. Not only is there tthread this class in VCL. Encapsulates the Windows API for multithreading. I think it's more convenient for him to offer
The ability to directly access objects in the main VCL thread. Can be very easy and the main thread in the form, control
Deal. There is not much difference between a single thread and a single-threaded approach. Just have multiple threads to access the main thread
(for example, to access Stringgrid on the same form). Just use the thread synchronize method to invoke the code that accesses the main VCL thread (see Help specifically), so we don't have to worry about the access violation. And for multithreading synchronization and mutual exclusion, CB also on window programming those mechanisms are encapsulated. For example, the criticalsection encapsulation of the critical area is tcriticalsection. Event events are encapsulated as tevent. These classes are fairly simple and easy to use.
The following is what I think is more important points for your reference.
1. The Waitfor method of TThread. is to wait for a thread to return. Its return value can be set arbitrarily in this line Chengri. So that the thread that calls him is aware of his operation when the thread returns.
Do thread cleanup work in the TThread onterminate event. He is not part of the thread running.
It is part of the main VCL thread. So local variables (such as int __thread i) in which thread cannot be accessed
You can write the clear code here without having to take care of where the Excute () method is now executed.
So it looks a bit like the role of a finally block in C + +.
2. Tevent is very important. Implement synchronization of threads. The WaitFor (int Timeout) feature is similar to the
WINDOW API WaitForSingleObject (). The return value includes:
Where the parameter timeout can be set to infinite for permanent wait, but this way, the program is easily dead here.
Wrsignaled This event occurs (successful return).
Wrtimeout wait for timeout.
Wrabandoned the event object has been destroyed before the timeout period for the event arrives.
Wrerror in the process of waiting for an exception to produce, to know the specific error to see Tevent lasterror
Property.