접기
#define END_PROCESS ( WM_USER +1 )
class CCriticalSectionDlg : public CDialog
{
// Construction
public:
CCriticalSectionDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CCriticalSectionDlg)
enum { IDD = IDD_CRITICALSECTION_DIALOG };
CButton m_ctrlDraw;
..
//}}AFX_DATA
..
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCriticalSectionDlg)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CCriticalSectionDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnClose();
//}}AFX_MSG
// <
afx_msg LRESULT OnEndProcess(WPARAM w, LPARAM l);
// >
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CCriticalSectionDlg, CDialog)
//{{AFX_MSG_MAP(CCriticalSectionDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
// <
ON_MESSAGE (END_PROCESS , OnEndProcess)
// >
END_MESSAGE_MAP()
..
UINT Thread_for_DataDraw(LPVOID pParam);
UINT Thread_for_DataReceive(LPVOID pParam);
CWinThread *g_pThread1, *g_pThread2;
volatile BOOL g_bContinue = TRUE;
const int NUMBER_OF_THREADS = 2;
HANDLE g_hThreads[NUMBER_OF_THREADS];
BOOL CCriticalSectionDlg::OnInitDialog()
{
..
SetIcon(m_hIcon, FALSE); // Set small icon
// <
g_pThread1 = AfxBeginThread (Thread_for_DataReceive, this, THREAD_PRIORITY_NORMAL , 0, CREATE_SUSPENDED , 0);
g_pThread1->m_bAutoDelete = false;
g_pThread1->ResumeThread ();
g_hThreads[0] = g_pThread1->m_hThread ;
g_pThread2 = AfxBeginThread(Thread_for_DataDraw, this, THREAD_PRIORITY_NORMAL , 0, CREATE_SUSPENDED , 0);
g_pThread2->m_bAutoDelete = false;
g_pThread2->ResumeThread ();
g_hThreads[1] = g_pThread2->m_hThread ;
// >
return TRUE; // return TRUE unless you set the focus to a control
}
..
UINT Thread_for_DataReceive(LPVOID pParam)
{
CCriticalSectionDlg *pWnd = (CCriticalSectionDlg *)pParam;
while(g_bContinue)
{
Sleep(0);
..
pWnd->m_ctrlReceive.SetCheck(1);
}
return 0;
}
UINT Thread_for_DataDraw(LPVOID pParam)
{
CCriticalSectionDlg *pWnd = (CCriticalSectionDlg *)pParam;
while(g_bContinue)
{
Sleep(0);
..
pWnd->m_ctrlReceive.SetCheck(0);
}
return 0;
}
UINT Thread_for_Stop(LPVOID pParam)
{
CCriticalSectionDlg *pWnd = (CCriticalSectionDlg *)pParam;
g_bContinue = FALSE;
WaitForMultipleObjects(NUMBER_OF_THREADS, g_hThreads, TRUE, INFINITE);
if (g_pThread1) {
delete g_pThread1;
g_pThread1 = NULL;
}
if (g_pThread2) {
delete g_pThread2;
g_pThread2 = NULL;
}
pWnd->PostMessage(END_PROCESS ,0,0);
return 0;
}
void CCriticalSectionDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
// <
AfxBeginThread (Thread_for_Stop, this);
// >
// CDialog::OnClose();
}
LRESULT CCriticalSectionDlg::OnEndProcess(WPARAM w, LPARAM l)
{
EndDialog(IDOK);
return 0;
}
BOOL CCriticalSectionDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
// <
switch(pMsg->message)
{
case WM_KEYDOWN:
{
switch(pMsg->wParam)
{
case VK_ESCAPE:
case VK_RETURN:
return TRUE;
}
}
}
// >
return CDialog::PreTranslateMessage(pMsg);
}
접기