www.pudn.com > MultiThread.rar > MultiThread2Dlg.cpp
// MultiThread2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MultiThread2.h"
#include "MultiThread2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/************************************************************************/
/* 打开ClassWizard,为编辑框IDC_COUNT添加int型变量m_nCount。
在MultiThread2Dlg.cpp文件中添加:
*/
/************************************************************************/
void ThreadFunc(int integer)
{
int i;
for(i=0;iLoadIcon(IDR_MAINFRAME);
}
void CMultiThread2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMultiThread2Dlg)
DDX_Text(pDX, IDC_COUNT, m_nCount);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMultiThread2Dlg, CDialog)
//{{AFX_MSG_MAP(CMultiThread2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultiThread2Dlg message handlers
BOOL CMultiThread2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMultiThread2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMultiThread2Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMultiThread2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//双击IDC_START按钮,完成该按钮的消息函数
void CMultiThread2Dlg::OnStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int integer=m_nCount;
hThread=CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFunc,
(VOID*)integer,
0,
&ThreadID
);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
/************************************************************************/
/* WaitForSingleObject函数,其函数原型为:
DWORD WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds);
hHandle为要监视的对象(一般为同步对象,也可以是线程)的句柄;
dwMilliseconds为hHandle对象所设置的超时值,单位为毫秒;
当在某一线程中调用该函数时,线程暂时挂起,系统监视hHandle所指向的对象的状态。
如果在挂起的dwMilliseconds毫秒内,线程所等待的对象变为有信号状态,则该函数立即返回;
如果超时时间已经到达dwMilliseconds毫秒,但hHandle所指向的对象还没有变成有信号状态,
函数照样返回。参数dwMilliseconds有两个具有特殊意义的值:0和INFINITE。
若为0,则该函数立即返回;若为INFINITE,则线程一直被挂起,
直到hHandle所指向的对象变为有信号状态时为止。
本例程调用该函数的作用是按下IDC_START按钮后,一直等到线程返回,
再恢复IDC_START按钮正常状态。编译运行该例程并细心体会。
*/
/************************************************************************/
WaitForSingleObject(hThread,INFINITE);
GetDlgItem(IDC_START)->EnableWindow(TRUE);
}