?? threadsynbyeventdlg.cpp
字號:
// ThreadSynByEventDlg.cpp : implementation file
//
#include "stdafx.h"
#include "evcstudy.h"
#include "ThreadSynByEventDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CThreadSynByEventDlg dialog
CThreadSynByEventDlg::CThreadSynByEventDlg(CWnd* pParent /*=NULL*/)
: CDialog(CThreadSynByEventDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CThreadSynByEventDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CThreadSynByEventDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CThreadSynByEventDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CThreadSynByEventDlg, CDialog)
//{{AFX_MSG_MAP(CThreadSynByEventDlg)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BTNEXEC, OnBtnexec)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CThreadSynByEventDlg message handlers
BOOL CThreadSynByEventDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
//創(chuàng)建線程同步事件句柄
m_hSynEvent = CreateEvent(NULL,false,true,NULL);
m_incNum = 0;
return TRUE; // return TRUE unless you set the focus to a control
}
/*
*函數(shù)介紹:線程執(zhí)行過程
*入口參數(shù):pArg:創(chuàng)建線程時,傳進(jìn)來的參數(shù),這里指的列表框控件指針
*出口參數(shù):(無)
*返回值:這里只返回1。
*/
DWORD CThreadSynByEventDlg::ThreadProc(PVOID pArg)
{
CThreadSynByEventDlg * pDlg;
CListBox * pLstBox;
pLstBox = (CListBox*)pArg;
pDlg = (CThreadSynByEventDlg*)AfxGetMainWnd();
TCHAR buffer[10];
//等待同步事件信號
if (WaitForSingleObject(pDlg->m_hSynEvent,INFINITE) == WAIT_OBJECT_0)
{
//給數(shù)組賦值
for (int i=0;i<MAXDATASIZE;i++)
{
pDlg->m_incNum++;
pDlg->m_aGlobalData[i] = pDlg->m_incNum;
Sleep(5);
}
//顯示已經(jīng)賦值的數(shù)組
for(i = 0 ; i < MAXDATASIZE ; i++)
{
_itow(pDlg->m_aGlobalData[i],buffer,10);
pLstBox->AddString(buffer);
}
}
//打開同步事件信號
SetEvent(pDlg->m_hSynEvent);
return 1;
}
void CThreadSynByEventDlg::OnDestroy()
{
CDialog::OnDestroy();
//關(guān)閉線程同步事件對象
CloseHandle(m_hSynEvent);
}
/*
執(zhí)行按鈕單擊事件,用于創(chuàng)建兩個線程
*/
void CThreadSynByEventDlg::OnBtnexec()
{
DWORD dwThreadId1,dwThreadId2;
HANDLE handle1,handle2;
CListBox * pLstOne;
CListBox * pLstTwo;
pLstOne = (CListBox*)GetDlgItem(IDC_LISTONE);
pLstTwo = (CListBox*)GetDlgItem(IDC_LISTTWO);
//創(chuàng)建兩個線程
handle1 = CreateThread(NULL,0,ThreadProc,pLstOne,0,&dwThreadId1);
handle2 = CreateThread(NULL,0,ThreadProc,pLstTwo,0,&dwThreadId2);
if (!handle1)
{
AfxMessageBox(_T("線程1創(chuàng)建失敗"));
}
if (!handle2)
{
AfxMessageBox(_T("線程2創(chuàng)建失敗"));
}
CloseHandle(handle1);
CloseHandle(handle2);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -