1)我新建了一个线程类,不知道什么时候线程才调用InitInstance方法。
   原以为是创建该线程对象时就调用,可是debug调式时没看它进去啊。
2)InitInstance方法中调用了WaitForSingleObject,那么CEvent.SetEvent()是该什么时候调用才好啊?

解决方案 »

  1.   

    要Run()方法里调用WaitForSingleObject吧
      

  2.   

    1.在你创建了线程类,然后使用CreateThread函数之后,才会进入InitInstance函数,也就是说InitInstance函数在你的线程函数内部调用。
      InitInstance返回真之后才进入Run函数之中,可以在InitInstance函数内调用WaitForSingleObject。
    2.既然你使用了WaitForSingleObject函数等待一个事件,那么事件激活必须在其他线程中了,也就是说CEvent.SetEvent()要在其他线程中调用。不能在你创建的线程中调用
      

  3.   

    首先谢谢您的解答,我明白你第一个回答了。
    关于您的第二个回答我不太理解,就是CEvent.SetEvent()起的作用是什么?
    难道是让WaitForSingleObject线程启动吗?
      

  4.   

    WaitForSingleObject是等待一个事件,在这个例子中,就是等待你给的CEvent,只有等到事件发后,他才会返回,当然,你也可以设定一个超时时间.可以看WaitForSingleObject的参数.
    CEvent::SetEvent
    BOOL SetEvent( );Return ValueNonzero if the function was successful, otherwise 0.ResSets the state of the event to signaled, releasing any waiting threads. If the event is manual, the event will remain signaled until ResetEvent is called. More than one thread can be released in this case. If the event is automatic, the event will remain signaled until a single thread is released. The system will then set the state of the event to nonsignaled. If no threads are waiting, the state remains signaled until one thread is released.是说SetEvent使CEvent处于一个有信号的状态