线程

    技术2022-05-11  101

     1.MFC将线程分为两类:用户界面(User interface)线程和工作者线程(Worker thread)。这两者之间的区别是

    用户界面线程有消息循环,而工作者线程没有。

    2.有两种方法用来在MFC应用程序中创建一个线程。可以构造一个CWinThread对象,并调用该对象的

    CreateThread()函数来创建线程,也可以使用AfxBeginThread()构造一个CWinThread对象并同时创建一个线程。

    3.工作者线程的创建形式:CWinThread * pThread = AfxBeginThread(ThreadFunc,&ThreadInfo,THREAD_PRIORITY_NOMAL,0,CREATE_SUSPENDED);

    4.创建一个用户界面线程与创建一个工作者线程的过程完全不同。一个工作者线程由它的线程函数定义,但是一个

    用户界面线程的行为由一个动态可以创建的类来控制,该类是从CWinThread派生的。

    5.一个运行的线程可以用CWinThread::SuspendThread()成员函数来挂起,并可以用

    CWinThread::ResumeThread()成员函数使它重新执行。一个线程可以在它自身上调用SuspendThread(),或者让

    另一个线程来调用它的SuspendThread()。然而,一个被挂起的线程不能调用ResumeThread()来唤醒自己,必

    须有别的线程为它调用ResumeThread()。

    6.一个线程一旦开始,它就可以以两种方式来终止。当一个工作者线程的线程函数执行一个返回语句或者调用

    AfxEndThread()成员函数时,这个工作者线程就终止。对于用户界面线程,当一个WM_QUIT消息发送到消息对列

    中,或者该线程中的一个函数调用AfxEndThread()成员函数时,该线程就被终止。

    7.worker Threads----These type of threads are used to complete background tasks when

    there is not a need for any user input.

    8. User Interface Threads------These types of threads are able to handle user input and

    they implement a message map to respond to events and messages generated by user

    interaction with the application. To create a user interface thread, you must derive your

    own class from CWinThread class and pass run-timeinformation about the class to the

    AfxBeginThread(...) function.

     


    最新回复(0)