📄 threadasyndlg.cpp
字号:
// ThreadAsynDlg.cpp : implementation file
//
#include "stdafx.h"
#include "evcstudy.h"
#include "ThreadAsynDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CThreadAsynDlg dialog
CThreadAsynDlg::CThreadAsynDlg(CWnd* pParent /*=NULL*/)
: CDialog(CThreadAsynDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CThreadAsynDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CThreadAsynDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//{{AFX_DATA_MAP(CThreadAsynDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CThreadAsynDlg, CDialog)
//{{AFX_MSG_MAP(CThreadAsynDlg)
ON_BN_CLICKED(IDC_BTNEXEC, OnBtnexec)
//ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CThreadAsynDlg message handlers
BOOL CThreadAsynDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
DWORD CThreadAsynDlg::ThreadProc(PVOID pArg)
{
CThreadAsynDlg * pDlg;
CListBox * pLstBox;
pLstBox = (CListBox*)pArg;
pDlg = (CThreadAsynDlg*)AfxGetMainWnd();
TCHAR buffer[10];
//给数组赋值
for (int i=0;i<MAXDATASIZE;i++)
{
pDlg->m_incNum++;
pDlg->m_aGlobalData[i] = pDlg->m_incNum;
Sleep(5);
}
//显示已经赋值的数组
for(i = 0 ; i < MAXDATASIZE ; i++)
{
_itow(pDlg->m_aGlobalData[i],buffer,10);
pLstBox->AddString(buffer);
}
return 1;
}
//void CThreadAsynDlg::OnDestroy()
//{
// CDialog::OnDestroy();
//}
void CThreadAsynDlg::OnBtnexec()
{
// TODO: Add your control notification handler code here
DWORD dwThreadId1,dwThreadId2;
HANDLE handle1,handle2;
CListBox * pLstOne;
CListBox * pLstTwo;
pLstOne = (CListBox*)GetDlgItem(IDC_LISTONE);
pLstTwo = (CListBox*)GetDlgItem(IDC_LISTTWO);
//初始化计数值
m_incNum = 0;
//创建两个线程
handle1 = CreateThread(NULL,0,ThreadProc,pLstOne,0,&dwThreadId1);
handle2 = CreateThread(NULL,0,ThreadProc,pLstTwo,0,&dwThreadId2);
if (!handle1)
{
AfxMessageBox(_T("线程1创建失败"));
}
if (!handle2)
{
AfxMessageBox(_T("线程2创建失败"));
}
CloseHandle(handle1);
CloseHandle(handle2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -