Again, this new version of the program seems to run just like the preceding version. However, the program is now using both event objects and user-defined Windows messages to communicate between the main program and the thread. No more messing with clunky global variables.One big difference from previous versions of the program is that the secondary thread is begun in the OnCreate() function, which is called when the application first runs and creates the view. However, because the first line of the thread function is the call to WaitForSingleObject(), the thread immediately suspends execution and waits for the threadStart event to be signaled.When the threadStart event object is signaled, the thread is free to display the message box and then enter its while loop, where it polls the threadEnd event object. The while loop continues to execute until threadEnd is signaled, at which time the thread sends the WM_THREADENDED message to the main program and exits. Because the thread is started in OnCreate(), after the thread ends, it can't be restarted.

解决方案 »

  1.   

    此外,程序的当前版本看起来和以前版本一样。程序依旧使用事件和用户自定义消息来保持主程序和线程之间的通讯。没有因为使用了笨拙的全局变量而使代码显得混乱。
    与前一版本程序的一个很大的区别是,第二个线程是在OnCreate函数里产生的,它在程序刚开始运行和产生视图后被调用。然而,由于线程函数了的第一条语句就是调用WaitForSingleObject函数,线程立即挂起,等待线程启动的事件的信号。
    当线程启动的事件被信号通知到,线程就可以显示它的消息框和进入循环,等待线程结束的事件。循环一直运行,直到线程结束的事件被信号通知到,这时,线程发送WM_THREADENDED消息到主程序,然后推出。由于线程是在OnCreate函数里启动的,因此,在线程结束后,它不可以被再次启动。
      

  2.   

    文笔粗陋,仅供参考:此外,程序的新版本运行时正像它的前一个版本一样.然而,现在,该程序既采用事件对象又采用用户自定义窗口消息进行主程序和线程的通信.不再受全局变量的干扰.程序新旧版本之间的一个巨大的差别在于:当应用程序首次运行并创建视图时,次线程始于函数OnCreate().然而,因为线程函数的第一行是WaitTorSingleObject()函数的调用,线程立刻延缓执行并等待threadStart事件向它发送信号.当threadStart事件对象发送信号,在显示消息框时线程是空闲的,然后线程进入它的while循环,在循环中激活threadEnd事件对象.while循环继续执行直到threadEnd事件对象发送信号.此时,线程发送WM_THREADENDED消息给主函数然后退出.因为线程始于函数OnCreate()函数,所以线程结束后就不能重新开始.
      

  3.   

    此外,请综合参考 ouyh12345()  tyl_22() 的回复:P