windows實現多線程編程方法「windows多線程編程技術與實例」

1 創建式程,編輯對話框資源

創建一個基於對話框的工程,添加控件,如下圖所示:

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

各控件ID及變量如下:

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

2 在頭文件中定義與線程相關變量

// Ch13Demo2Dlg.h

typedef struct Threadinfo{

CProgressCtrl *progress;//進度條對象

int speed;//進度條速度

int pos;//進度條位置

} thread,*lpthread;

class CCh13Demo2Dlg : public CDialog

{

……

protected:

HICON m_hIcon;

thread thread1;//線程1的結構

thread thread2;//線程2的結構

thread thread3;//線程3的結構

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

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

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

定義線程入口函數

// Ch13Demo2Dlg.cpp

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

{

lpthread temp=(lpthread)pthread;//進度條結構體

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

while(temp->pos<20)

{

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

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

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

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);//設置進度條範圍

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

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

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

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

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

return TRUE;

}

// Ch13Demo2Dlg.cpp

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

{

lpthread temp=(lpthread)pthread;//進度條結構體

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

while(temp->pos<20)

{

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

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

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

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;

//生成線程參數

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);//創建並開始線程

}

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;//線程結構

thread2.speed=200;

thread2.pos=0;

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

{

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

}

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;//線程結構

thread3.speed=200;

thread3.pos=0;

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

{

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

}

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|一個實例理解多線程編程

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/284431.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-22 15:07
下一篇 2024-12-22 15:07

相關推薦

發表回復

登錄後才能評論