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

📄 ex_threadview.cpp

📁 郑阿齐VC教材
💻 CPP
字号:
// Ex_ThreadView.cpp : implementation of the CEx_ThreadView class
//

#include "stdafx.h"
#include "Ex_Thread.h"

#include "Ex_ThreadDoc.h"
#include "Ex_ThreadView.h"

#include <afxmt.h>

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView

IMPLEMENT_DYNCREATE(CEx_ThreadView, CView)

BEGIN_MESSAGE_MAP(CEx_ThreadView, CView)
	//{{AFX_MSG_MAP(CEx_ThreadView)
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_MESSAGE(WM_USERMSG, OnUserMessage)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView construction/destruction

CEx_ThreadView::CEx_ThreadView()
{
	// TODO: add construction code here

}

CEx_ThreadView::~CEx_ThreadView()
{
}

BOOL CEx_ThreadView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView drawing

void CEx_ThreadView::OnDraw(CDC* pDC)
{
	CEx_ThreadDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView printing

BOOL CEx_ThreadView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEx_ThreadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEx_ThreadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView diagnostics

#ifdef _DEBUG
void CEx_ThreadView::AssertValid() const
{
	CView::AssertValid();
}

void CEx_ThreadView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CEx_ThreadDoc* CEx_ThreadView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx_ThreadDoc)));
	return (CEx_ThreadDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEx_ThreadView message handlers
CSemaphore *semaphore = new CSemaphore(2,2);

//////////////////////////////////////////////////////////////////////
UINT ThreadProc1(LPVOID param)
{
CSingleLock singleLock(semaphore);
singleLock.Lock();
Sleep(10000);
::MessageBox((HWND)param, "线程1访问!", "线程1", MB_OK);
return 0;
}
UINT ThreadProc2(LPVOID param)
{
CSingleLock singleLock(semaphore);
singleLock.Lock();
Sleep(10000);
::MessageBox((HWND)param, "线程2访问!", "线程2", MB_OK);
return 0;
}
UINT ThreadProc3(LPVOID param)
{
CSingleLock singleLock(semaphore);
singleLock.Lock();
Sleep(10000);
::MessageBox((HWND)param, "线程3访问!", "线程3", MB_OK);
return 0;
}  

CWinThread	*pThread;
HWND		hWnd;
BOOL  bEnd = FALSE;		// 定义的全局变量,用于控制线程的运行
CEvent eStart, eEnd;
UINT MyThreadFunc(LPVOID pParam)	// 线程函数
{
	::WaitForSingleObject(eStart.m_hObject, INFINITE);
	AfxMessageBox("线程开始!");
	while(!bEnd){
		Beep(100,100);	Sleep(1000);
		int result = ::WaitForSingleObject(eEnd.m_hObject,0);
		// 等待threadEnd事件有信号,无信号时线程在这里悬停
		if ( result == WAIT_OBJECT_0) bEnd = TRUE;
	}
	::PostMessage(hWnd, WM_USERMSG, 0, 0);	// 发出用户消息
	return 0;
}
void CEx_ThreadView::OnInitialUpdate() 
{
	hWnd = GetSafeHwnd();
	eStart.SetEvent();								// teStart事件有信号
	pThread = AfxBeginThread(MyThreadFunc, hWnd);	// 启动线程
	pThread->m_bAutoDelete = FALSE;					// 线程设为手动删除

	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
HWND hWnd = GetSafeHwnd();
AfxBeginThread(ThreadProc1, hWnd);
AfxBeginThread(ThreadProc2, hWnd);
AfxBeginThread(ThreadProc3, hWnd);
	
}

void CEx_ThreadView::OnDestroy() 
{
	bEnd = TRUE;		// 改变变量,线程结束
	WaitForSingleObject(pThread->m_hThread, INFINITE);	// 等待线程结束
	delete pThread;		//删除线程
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	
}
LRESULT  CEx_ThreadView::OnUserMessage(WPARAM wParam,LPARAM lParam)
{
 	AfxMessageBox("线程已经终止!");
	return 0;
}

void CEx_ThreadView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	eEnd.SetEvent();		// eStart事件有信号
	CView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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