你在delphi的帮助中的索引中找“tevent”,找到后有一项Using TEvent,上面有一个程序代码例子如下: Sometimes, you need to wait for a thread to finish some operation rather than waiting for a particular thread to complete execution. To do this, use an event object. Event objects (TEvent) should be created with global scope so that they can act like signals that are visible to all threads. When a thread completes an operation that other threads depend on, it calls TEvent.SetEvent. SetEvent turns on the signal, so any other thread that checks will know that the operation has completed. To turn off the signal, use the ResetEvent method.For example, consider a situation where you must wait for several threads to complete their execution rather than a single thread. Because you don抰 know which thread will finish last, you can抰 simply use the WaitFor method of one of the threads. Instead, you can have each thread increment a counter when it is finished, and have the last thread signal that they are all done by setting an event. The following code shows the end of the OnTerminate event handler for all of the threads that must complete. CounterGuard is a global critical section object that prevents multiple threads from using the counter at the same time. Counter is a global variable that counts the number of threads that have completed.procedure TDataModule.TaskThreadTerminate(Sender: TObject);begin ... CounterGuard.Acquire; { obtain a lock on the counter } Dec(Counter); { decrement the global counter variable } if Counter = 0 then Event1.SetEvent; { signal if this is the last thread } CounterGuard.Release; { release the lock on the counter } ... end;The main thread initializes the Counter variable, launches the task threads, and waits for the signal that they are all done by calling the WaitFor method. WaitFor waits for a specified time period for the signal to be set, and returns one of the values from the following table:Value Meaning wrSignaled The signal of the event was set. wrTimeout The specified time elapsed without the signal being set. wrAbandoned The event object was destroyed before the timeout period elapsed. wrError An error occurred while waiting. The following shows how the main thread launches the task threads and then resumes when they have all completed:Event1.ResetEvent; { clear the event before launching the threads }for i := 1 to Counter do TaskThread.Create(False); { create and launch task threads } if Event1.WaitFor(20000) <> wrSignaled then raise Exception; { now continue with the main thread. All task threads have finished }Note: If you do not want to stop waiting for an event after a specified time period, pass the WaitFor method a parameter value of INFINITE. Be careful when using INFINITE, because your thread will hang if the anticipated signal is never received.
这样生产线程先reset一下Event,Post消息后再waitfor这个Event,消费线程访问完数据后set这个Event应该就可以了。:-)
Sometimes, you need to wait for a thread to finish some operation rather than waiting for a particular thread to complete execution. To do this, use an event object. Event objects (TEvent) should be created with global scope so that they can act like signals that are visible to all threads.
When a thread completes an operation that other threads depend on, it calls TEvent.SetEvent. SetEvent turns on the signal, so any other thread that checks will know that the operation has completed. To turn off the signal, use the ResetEvent method.For example, consider a situation where you must wait for several threads to complete their execution rather than a single thread. Because you don抰 know which thread will finish last, you can抰 simply use the WaitFor method of one of the threads. Instead, you can have each thread increment a counter when it is finished, and have the last thread signal that they are all done by setting an event.
The following code shows the end of the OnTerminate event handler for all of the threads that must complete. CounterGuard is a global critical section object that prevents multiple threads from using the counter at the same time. Counter is a global variable that counts the number of threads that have completed.procedure TDataModule.TaskThreadTerminate(Sender: TObject);begin
...
CounterGuard.Acquire; { obtain a lock on the counter }
Dec(Counter); { decrement the global counter variable }
if Counter = 0 then
Event1.SetEvent; { signal if this is the last thread }
CounterGuard.Release; { release the lock on the counter }
...
end;The main thread initializes the Counter variable, launches the task threads, and waits for the signal that they are all done by calling the WaitFor method. WaitFor waits for a specified time period for the signal to be set, and returns one of the values from the following table:Value Meaning
wrSignaled The signal of the event was set.
wrTimeout The specified time elapsed without the signal being set.
wrAbandoned The event object was destroyed before the timeout period elapsed.
wrError An error occurred while waiting.
The following shows how the main thread launches the task threads and then resumes when they have all completed:Event1.ResetEvent; { clear the event before launching the threads }for i := 1 to Counter do
TaskThread.Create(False); { create and launch task threads }
if Event1.WaitFor(20000) <> wrSignaled then
raise Exception;
{ now continue with the main thread. All task threads have finished }Note: If you do not want to stop waiting for an event after a specified time period, pass the WaitFor method a parameter value of INFINITE. Be careful when using INFINITE, because your thread will hang if the anticipated signal is never received.
对了,你能再解释一下“因为这个时候是两两数据交换”是指什么情况吗?