資源簡介
1. 創(chuàng)建一個基于對話框的應用程序。并增加如圖所示控件;分別為3個進度條控件關聯(lián)三個進度條類型的變量;并在對話框的初始化函數(shù)中,設定進度條的范圍;為編輯框關聯(lián)一個整型的變量;為12個按鈕添加消息處理函數(shù);
2. 定義結構體:用做線程函數(shù)的參數(shù)傳遞
typedef struct Threadinfo{
CProgressCtrl *progress;//進度條對象
int speed; //進度條速度
int pos; //進度條位置
} thread,*lpthread;
3. 為對話框增加三個句柄,用于標識各個線程;
HANDLE hThread1; //線程1線程句柄
HANDLE hThread2; //線程2線程句柄
HANDLE hThread3; //線程3線程句柄
在增加三個結構體類型的變量,用做線程函數(shù)的參數(shù)傳遞;
HANDLE hThread1; //線程1線程句柄
HANDLE hThread2; //線程2線程句柄
HANDLE hThread3; //線程3線程句柄
4. 新增一個靜態(tài)的全局變量,用于記錄所有線程的狀態(tài):static int GlobalVar=10000;
5. 聲明并編寫線程函數(shù),注意只能有一個參數(shù),且函數(shù)的返回值類型也是固定的;函數(shù)名可以自定義;
DWORD WINAPI ThreadFun(LPVOID pthread);//線程入口函數(shù)
6. 在啟動按鈕的消息處理函數(shù)中編寫如下代碼:
thread1.progress=&m_progress1;//進度條對象
thread1.speed=100;//速度
thread1.pos=0;//初始位置
hThread1=CreateThread(NULL,0,ThreadFun,&thread1;,0,0);//創(chuàng)建并開始線程
if (!hThread1)
{
MessageBox("創(chuàng)建線程失敗");
}
7. 編寫線程函數(shù)(一般是一個死循環(huán),或者需要花費時間很長的算法!否者就失去了多線程的意義)
DWORD WINAPI ThreadFun(LPVOID pthread) //線程入口函數(shù)
{
lpthread temp=(lpthread)pthread;//參數(shù)強制轉(zhuǎn)換為結構體類型
temp->progress->SetPos(temp->pos); //設置被傳遞過來的進度條的位置
while(temp->posspeed); /設置速度
temp->pos++; //增加進度
temp->progress->SetPos(temp->pos); //設置進度條的新位置
GlobalVar--;
if(temp->pos==20)
{
temp->pos=0; //進度條滿則歸0
}
}
return true;
}
8. 在掛起按鈕函數(shù)中,編寫如下代碼:
if(SuspendThread(hThread1)==0xFFFFFFFF)
{
MessageBox("掛起失敗!進程可能已經(jīng)死亡或未創(chuàng)建!");
return;
}
9. 在執(zhí)行按鈕函數(shù)中,編寫如下代碼:
if(ResumeThread(hThread1)==0xFFFFFFFF)
{
MessageBox("執(zhí)行失敗!進程可能已經(jīng)死亡或未創(chuàng)建!");
return;
}
10. 在停止按鈕函數(shù)中,編寫如下代碼:
if(TerminateThread(hThread1,0))//前些終止線程
{
CloseHandle(hThread1);//銷毀線程句柄
}
else
{
MessageBox("終止進程失敗!");
}
11. 為應用程序添加WM_TIMER消息,實時更新全局變量的值到編輯框;

代碼片段和文件信息
//?CreateThread.cpp?:?Defines?the?class?behaviors?for?the?application.
//
#include?“stdafx.h“
#include?“CreateThread.h“
#include?“CreateThreadDlg.h“
#ifdef?_DEBUG
#define?new?DEBUG_NEW
#undef?THIS_FILE
static?char?THIS_FILE[]?=?__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//?CCreateThreadApp
BEGIN_MESSAGE_MAP(CCreateThreadApp?CWinApp)
//{{AFX_MSG_MAP(CCreateThreadApp)
//?NOTE?-?the?ClassWizard?will?add?and?remove?mapping?macros?here.
//????DO?NOT?EDIT?what?you?see?in?these?blocks?of?generated?code!
//}}AFX_MSG
ON_COMMAND(ID_HELP?CWinApp::onhelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//?CCreateThreadApp?construction
CCreateThreadApp::CCreateThreadApp()
{
//?TODO:?add?construction?code?here
//?Place?all?significant?initialization?in?InitInstance
}
/////////////////////////////////////////////////////////////////////////////
//?The?one?and?only?CCreateThreadApp?object
CCreateThreadApp?theApp;
/////////////////////////////////////////////////////////////////////////////
//?CCreateThreadApp?initialization
BOOL?CCreateThreadApp::InitInstance()
{
AfxEnableControlContainer();
//?Standard?initialization
//?If?you?are?not?using?these?features?and?wish?to?reduce?the?size
//??of?your?final?executable?you?should?remove?from?the?following
//??the?specific?initialization?routines?you?do?not?need.
#ifdef?_AFXDLL
Enable3dControls(); //?Call?this?when?using?MFC?in?a?shared?DLL
#else
Enable3dControlsStatic(); //?Call?this?when?linking?to?MFC?statically
#endif
CCreateThreadDlg?dlg;
m_pMainWnd?=?&dlg;
int?nResponse?=?dlg.DoModal();
if?(nResponse?==?IDOK)
{
//?TODO:?Place?code?here?to?handle?when?the?dialog?is
//??dismissed?with?OK
}
else?if?(nResponse?==?IDCANCEL)
{
//?TODO:?Place?code?here?to?handle?when?the?dialog?is
//??dismissed?with?Cancel
}
//?Since?the?dialog?has?been?closed?return?FALSE?so?that?we?exit?the
//??application?rather?than?start?the?application‘s?message?pump.
return?FALSE;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2012-03-21?14:32??CreateThread\
?????文件???????37052??2012-03-21?15:14??CreateThread\CreateThread.aps
?????文件????????1892??2012-03-21?16:25??CreateThread\CreateThread.clw
?????文件????????2147??2008-05-07?08:58??CreateThread\CreateThread.cpp
?????文件????????4267??2008-05-07?08:58??CreateThread\CreateThread.dsp
?????文件?????????547??2008-05-07?08:58??CreateThread\CreateThread.dsw
?????文件????????1390??2008-05-07?08:58??CreateThread\CreateThread.h
?????文件???????66560??2012-03-21?16:25??CreateThread\CreateThread.ncb
?????文件???????48640??2012-03-21?16:25??CreateThread\CreateThread.opt
?????文件?????????717??2012-03-21?16:16??CreateThread\CreateThread.plg
?????文件????????6390??2012-03-21?15:14??CreateThread\CreateThread.rc
?????文件????????9833??2012-03-21?16:17??CreateThread\CreateThreadDLG.cpp
?????文件????????2232??2012-03-21?15:41??CreateThread\CreateThreadDLG.h
?????文件????????3687??2008-05-07?08:58??CreateThread\ReadMe.txt
?????目錄???????????0??2012-03-21?14:32??CreateThread\res\
?????文件????????1380??2012-03-21?15:14??CreateThread\Resource.h
?????文件????????1078??2008-05-07?08:58??CreateThread\res\CreateThread.ico
?????文件?????????404??2008-05-07?08:58??CreateThread\res\CreateThread.rc2
?????文件?????????214??2008-05-07?08:58??CreateThread\StdAfx.cpp
?????文件????????1054??2008-05-07?08:58??CreateThread\StdAfx.h
評論
共有 條評論