⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wqdemodlg.cpp

📁 多线程的运用
💻 CPP
字号:
// WQDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WQDemo.h"
#include "WQDemoDlg.h"
#include "WorkQueue.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


class SpecificWorkItem : public WorkItemBase
{

public:
	SpecificWorkItem(CXListCtrl* pListCtrl,int nItem){m_pListCtrl = pListCtrl; m_nItem = nItem;}
private:
	void   DoWork(void* pThreadContext);
    void   Abort();
	int    m_nItem;
	CXListCtrl* m_pListCtrl;

};

void SpecificWorkItem::DoWork(void* pThreadContext)
{

	m_pListCtrl->EnsureVisible(m_nItem,TRUE);
	m_pListCtrl->SetProgress(m_nItem,0);

	for(int i = 0 ; i < 100 ; i++)
	{	
		Sleep(100);
		m_pListCtrl->UpdateProgress(m_nItem,0,i);
	}

	m_pListCtrl->DeleteProgress(m_nItem,0);

	m_pListCtrl->SetItemText(m_nItem,0,_T("Complete"));	

	delete this;

}

void SpecificWorkItem::Abort()
{
	m_pListCtrl->SetItemText(m_nItem,0,_T("Aborted"));
	delete this;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWQDemoDlg dialog

unsigned long __stdcall CWQDemoDlg::ThreadFuncDestroy( void*  pParam )
{

	CWQDemoDlg* pDlg = (CWQDemoDlg*)pParam;
	pDlg->m_WorkQueue.Destroy();
	return 1;
}


CWQDemoDlg::CWQDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWQDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWQDemoDlg)
	m_nNumberOfThreads = 5;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWQDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWQDemoDlg)
	DDX_Control(pDX, IDC_ITEM_QUEUE_LIST, m_ItemsOueueList);
	DDX_Text(pDX, IDC_NUMBER_OF_THREADS_EDIT, m_nNumberOfThreads);
	DDV_MinMaxUInt(pDX, m_nNumberOfThreads, 0, 64);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWQDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CWQDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CREATE, OnCreate)
	ON_BN_CLICKED(IDC_DESTROY, OnDestroy)
	ON_BN_CLICKED(IDC_ADD_ITEM_BTN, OnAddItemBtn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWQDemoDlg message handlers

BOOL CWQDemoDlg::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

	m_ItemsOueueList.InsertColumn(0,_T("项目进程"),LVCFMT_LEFT,195);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWQDemoDlg::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 CWQDemoDlg::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 CWQDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWQDemoDlg::OnCreate() 
{
    //界面更新显示
	UpdateData();
	GetDlgItem(IDC_CREATE)->EnableWindow(FALSE);
	GetDlgItem(IDC_NUMBER_OF_THREADS_SPIN_SPIN)->EnableWindow(FALSE);
	GetDlgItem(IDC_DESTROY)->EnableWindow(TRUE);
	GetDlgItem(IDC_ADD_ITEM_BTN)->EnableWindow(TRUE);	
	//创建线程
	m_WorkQueue.Create(m_nNumberOfThreads);	
}

void CWQDemoDlg::OnDestroy() 
{
	
	GetDlgItem(IDC_DESTROY)->EnableWindow(FALSE);
	GetDlgItem(IDC_ADD_ITEM_BTN)->EnableWindow(FALSE);

	//为了在程序退出时界面不被冻结,这里建立了另一个线程,利用该线程去结束其他的线程

	DWORD dwThreadId;
	HANDLE hThreade = CreateThread(NULL,
		0,
		CWQDemoDlg::ThreadFuncDestroy,
		this,
		0,
		&dwThreadId);


		while (TRUE)
		{
			DWORD result ; 
			MSG msg ; 
			
			// 读取所有的消息 
			// 移除读到的所有消息,防止界面锁住
			while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
			{ 
				if (msg.message == WM_QUIT)  
					return; 
				DispatchMessage(&msg); 
			} 			
			// 等待任何队列消息或则hThreade线程句柄触发
			result = MsgWaitForMultipleObjects(1, &hThreade , 
				FALSE, INFINITE, QS_ALLINPUT); 
			
			// 如果是hThreade线程结束,表示所有的线程都已经退出
			if (result == WAIT_OBJECT_0)
			{
				break;
			} 
			else 
			{ 
				continue;
					
			} 
		} 
		
		GetDlgItem(IDC_CREATE)->EnableWindow(TRUE);
		GetDlgItem(IDC_NUMBER_OF_THREADS_SPIN_SPIN)->EnableWindow(TRUE);
		
}

void CWQDemoDlg::OnAddItemBtn() 
{
	static i = 0;
	i++;
	int nItem = m_ItemsOueueList.InsertItem(i, _T("Waiting..."));
	m_WorkQueue.InsertWorkItem(new SpecificWorkItem(&m_ItemsOueueList,nItem));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -