bro 조경민 bro@shinbiro.com
99.6.10 CWinThread 간단 사용
--------------------------------------------------------

Class Wizard로 CWinThread를 상속받은 MyThread를 만든다.
그리고 dlg같은 스레드를 쓰려는 클래스의 맴버변수로
CWinThread* m_pWinThread; 를 만든다.

다음은 스레드 생성

m_pWinThread = AfxBeginThread(RUNTIME_CLASS(MyThread));

다음은 윈스레드의 메세지 핸들링 방법이다.

MyThread가 받을 메세지로 TM_MYMSG라는 것이

Resource.h에 있다고 치자. 없으면 쓰자

#define TM_MYMSG                       700

그리고 MyThread.h에 void MyMsg();를 하고

BEGIN_MESSAGE_MAP(MyThread, CWinThread)
        //{{AFX_MSG_MAP(TestWinThread)
                // NOTE - the ClassWizard will add and
                // remove mapping macros here.
        //}}AFX_MSG_MAP
        ON_THREAD_MESSAGE(TM_MYMSG,MyMsg)
END_MESSAGE_MAP()

이렇게 하면  TM_MYMSG가 윈스레드로 오면 MyMsg() 함수가 불리게 된다.

dlg에서
((MyThread*)m_pWinThread)->PostThreadMessage(TM_MYMSG,0,0);

다음은 윈스레드를 죽이는 방법이다.

((MyThread*)m_pWinThread)->PostThreadMessage(WM_QUIT,0,0);        

이렇게 하면 죽게 된다.

그리고 한번 AfxBeginThread로 생기게 되면 속안에서

CreateThread를 호출하면서 스레드 객체가 생기며 CWinThread의

경우 Run함수가 호출된다. CwinThread::Run안에서 메세지 루프가 돌게 된다.

'KB > MFC/Win32' 카테고리의 다른 글

DLL을 사용하자  (0) 2004.03.19
다이얼로그 베이스 DB프로그램 짜기  (0) 2004.03.19
hook 설명  (0) 2004.03.19
템플리트클래스..  (0) 2004.03.19
템플리트 in DLL  (0) 2004.03.19

+ Recent posts