用TEvent能解决你的问题,等到事件发生时,被阻塞的线程就唤醒,我在vc中作过。

解决方案 »

  1.   

    我的通讯线程是不阻塞的,没数据,则继续与客户交流,用TEvent怎么实现呢?
      

  2.   

    我想你可以在通讯线程中建立一个消息循环,一边处理socket通讯,一边接收windows消息,然后生产线程读出数据后向消费线程PostThreadMessage,消息中带一个Event的句柄和数据块的地址,由于实在同一个进程中,地址和句柄可以在多个线程中有效。
    这样生产线程先reset一下Event,Post消息后再waitfor这个Event,消费线程访问完数据后set这个Event应该就可以了。:-)
      

  3.   

    你在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.
      

  4.   

    这样就比较麻烦了,你还不如用临界区呢,其实用 TCriticalSection 也挺好的呀,这样只有在其它线程已经进入的情况下才会等待。
    对了,你能再解释一下“因为这个时候是两两数据交换”是指什么情况吗?