1. 河豚號 > 生活百科 >

windows多線程編程技術(shù)與實例(windows實現(xiàn)多線程編程方法)

1 創(chuàng)建式程,編輯對話框資源

創(chuàng)建一個基于對話框的工程,添加控件,如下圖所示:

 

VC|一個實例理解多線程編程

 

各控件ID及變量如下:

 

VC|一個實例理解多線程編程

 

2 在頭文件中定義與線程相關(guān)變量

// Ch13Demo2Dlg.h

typedef struct Threadinfo{

CProgressCtrl *progress;//進度條對象

int speed;//進度條速度

int pos;//進度條位置

} thread,*lpthread;

class CCh13Demo2Dlg : public CDialog

{

……

protected:

HICON m_hIcon;

thread thread1;//線程1的結(jié)構(gòu)

thread thread2;//線程2的結(jié)構(gòu)

thread thread3;//線程3的結(jié)構(gòu)

HANDLE hThread1;//線程1線程句柄

HANDLE hThread2;//線程2線程句柄

HANDLE hThread3;//線程3線程句柄

定義線程入口函數(shù)

// Ch13Demo2Dlg.cpp

DWORD WINAPI ThreadFun(LPVOID pthread)//線程入口函數(shù)

{

lpthread temp=(lpthread)pthread;//進度條結(jié)構(gòu)體

temp->progress->SetPos(temp->pos);

while(temp->pos<20)

{

Sleep(temp->speed);//設(shè)置速度

temp->pos++;//增加進度

temp->progress->SetPos(temp->pos);//設(shè)置進度條的新位置

if(temp->pos==20)

{

temp->pos=0;//進度條滿則歸0

}

}

return true;

}

3 對話框控件初始化

// Ch13Demo2Dlg.cpp

BOOL CCh13Demo2Dlg::OnInitDialog()

{

BOOL CCh13Demo2Dlg::OnInitDialog()

{

CDialog::OnInitDialog();

……

m_progress1.SetRange(0,20);//設(shè)置進度條范圍

m_progress2.SetRange(0,20);//設(shè)置進度條范圍

m_progress3.SetRange(0,20);//設(shè)置進度條范圍

GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按鈕無效

GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按鈕無效

GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按鈕無效

return TRUE;

}

// Ch13Demo2Dlg.cpp

DWORD WINAPI ThreadFun(LPVOID pthread)//線程入口函數(shù)

{

lpthread temp=(lpthread)pthread;//進度條結(jié)構(gòu)體

temp->progress->SetPos(temp->pos);

while(temp->pos<20)

{

Sleep(temp->speed);//設(shè)置速度

temp->pos++;//增加進度

temp->progress->SetPos(temp->pos);//設(shè)置進度條的新位置

if(temp->pos==20)

{

temp->pos=0;//進度條滿則歸0

}

}

return true;

}

void CCh13Demo2Dlg::OnStar1()

{

// TODO: Add your control notification handler code here

DWORD ThreadID;

DWORD code;

//生成線程參數(shù)

thread1.progress=&m_progress1;//進度條對象

thread1.speed=100;//速度

thread1.pos=0;//初始位置

if(!GetExitCodeThread(hThread1,&code)||(code!=STILL_ACTIVE))

{

hThread1=CreateThread(NULL,0,ThreadFun,&thread1,0,&ThreadID);//創(chuàng)建并開始線程

}

GetDlgItem(IDC_PAUSE1)->EnableWindow(TRUE);//停止按鈕生效

GetDlgItem(IDC_STAR1)->EnableWindow(FALSE);//開始按鈕無效

}

void CCh13Demo2Dlg::OnStar2()

{

// TODO: Add your control notification handler code here

DWORD ThreadID;

DWORD code;

//生成線程

thread2.progress=&m_progress2;//線程結(jié)構(gòu)

thread2.speed=200;

thread2.pos=0;

if(!GetExitCodeThread(hThread2,&code)||(code!=STILL_ACTIVE))

{

hThread2=CreateThread(NULL,0,ThreadFun,&thread2,0,&ThreadID);//創(chuàng)建線程

}

GetDlgItem(IDC_PAUSE2)->EnableWindow(TRUE);//停止按鈕生效

GetDlgItem(IDC_STAR2)->EnableWindow(FALSE);//開始按鈕無效

}

void CCh13Demo2Dlg::OnStar3()

{

// TODO: Add your control notification handler code here

DWORD ThreadID;

DWORD code;

//生成線程

thread3.progress=&m_progress3;//線程結(jié)構(gòu)

thread3.speed=200;

thread3.pos=0;

if(!GetExitCodeThread(hThread3,&code)||(code!=STILL_ACTIVE))

{

hThread3=CreateThread(NULL,0,ThreadFun,&thread3,0,&ThreadID);//創(chuàng)建線程

}

GetDlgItem(IDC_PAUSE3)->EnableWindow(TRUE);//停止按鈕生效

GetDlgItem(IDC_STAR3)->EnableWindow(FALSE);//開始按鈕無效

}

void CCh13Demo2Dlg::OnPause1()

{

// TODO: Add your control notification handler code here

DWORD code;

if(GetExitCodeThread(hThread1,&code))

if(code==STILL_ACTIVE)//如果當前線程還活動

{

TerminateThread(hThread1,0);//前些終止線程

CloseHandle(hThread1);//銷毀線程句柄

}

GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按鈕無效

GetDlgItem(IDC_STAR1)->EnableWindow(TRUE);//開始按鈕生效

}

void CCh13Demo2Dlg::OnPause2()

{

// TODO: Add your control notification handler code here

DWORD code;

if(GetExitCodeThread(hThread2,&code))

if(code==STILL_ACTIVE)

{

TerminateThread(hThread2,0);

CloseHandle(hThread2);

}

GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按鈕無效

GetDlgItem(IDC_STAR2)->EnableWindow(TRUE);//開始按鈕生效

}

void CCh13Demo2Dlg::OnPause3()

{

// TODO: Add your control notification handler code here

DWORD code;

if(GetExitCodeThread(hThread3,&code))

if(code==STILL_ACTIVE)

{

TerminateThread(hThread3,0);

CloseHandle(hThread2);

}

GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按鈕無效

GetDlgItem(IDC_STAR3)->EnableWindow(TRUE);//開始按鈕生效

}

 

VC|一個實例理解多線程編程

 

本文由網(wǎng)上采集發(fā)布,不代表我們立場,轉(zhuǎn)載聯(lián)系作者并注明出處:http://m.zltfw.cn/shbk/37683.html

聯(lián)系我們

在線咨詢:點擊這里給我發(fā)消息

微信號:15705946153

工作日:9:30-18:30,節(jié)假日休息