1. 전역으로 Thread 작업 함수 선언

UINT ThreadTaskFunc(LPVOID pParam);

 

 

2. Thread 작업 함수에 다음 code를 기본적으로 작성

UINT ThreadTaskFunc(LPVOID pParam)

{

//본 함수는 전역함수 이므로, Dialog Class 의 멤버에 접근하기 위한 포인터 선언

// 이후, pDlg-> 를 사용하여 Dialog 의 멤버에 접근 가능하다.

CProjectDlg* pDlg = (CProjectDlg*)pParam;

 

// 추후, thread 를 안정적으로 종료시키기 위해 m_flag_threadrun 를 Flase 로 만듦.

while(pDlg->m_flag_threadrun)

{

//Thread 작업

}

 

return 0;

}

 

 

3. Thread 시작

// m_flag_threadrun : Dialog class에 정의된 멤버변수

// m_pThread : thread 의 pointer

// CWinThread* m_pThread;

 

//thread 작업 함수가 while() 로 계속 돌아가도록 함.

m_flag_threadrun=TRUE;

 

//thread 시작

m_pThread=AfxBeginThread(ThreadTaskFunc,this);

if(m_pThread==NULL)

{

AfxMessageBox("ERROR: Failed to begin thread");

return;

}

 

 

4. thread 종료

//thread 종료 flag

m_flag_threadrun=FALSE;

 

//thread 가 종료 될 때 까지 기다림

WaitForSingleObject( m_pThread->m_hThread, INFINITE );

 

 

5. 프로그램이 종료 되는 시점에서 thread 종료 code 추가

DestroyWindow() 를 재정의 하고 (Dlg 클래스에서, 속성창에서, 재정의, DestroyWindow

생성된 재정의 함수에 아래 code추가

 

if(m_flag_threadrun=TRUE)

{

[4. thread 종료] 참고

}

 

+ Recent posts