【VC开源代码栏目提醒】:网学会员VC开源代码为您提供Ex15_2View.cpp参考,解决您在Ex15_2View.cpp学习中工作中的难题,参考学习。
// Ex15_2View.cpp : implementation of the CEx15_2View class
//
#include "stdafx.h"
#include "Ex15_2.h"
#include "Ex15_2Doc.h"
#include "Ex15_2View.h"
//#include "process.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_USER_MSG WM_USER + 101// 自定义消息
//线程处理函数
UINT ThreadFunc1(LPVOID pParam)
{
AfxMessageBox("AfxBeginThread()函数创建的线程!");// 显示消息对话框
return 0;
}
DWORD WINAPI ThreadFunc2(LPVOID pParam)
{
AfxMessageBox("CreateThread()函数创建的线程!");// 显示消息对话框
return 0;
}
//线程间
通信用的处理函数
volatile bool g_bDo = false;
UINT ThreadFunc3(LPVOID pParam)
{
while (g_bDo)
{
Sleep(3000); // 延迟3秒
AfxMessageBox("使用全局变量,线程正在运行!");
}
AfxMessageBox("使用全局变量,线程终止运行!");
return 0;
}
//
UINT ThreadFunc4(LPVOID pParam) //线程函数
{ Sleep(3000); // 延迟3秒
::PostMessage((HWND)pParam, WM_USER_MSG, 0, 0); // 向主线程发送自定义消息
return 0;
}
//
HANDLE hEvent = NULL; // 事件句柄
UINT ThreadFunc5(LPVOID pParam)//线程处理函数
{
while(true)
{ // 等待事件发生
DWORD dwRet = WaitForSingleObject(hEvent, 0);
// 如果事件置位则退出线程,否则将继续执行
if (dwRet == WAIT_OBJECT_0)
break;
else
{ Sleep(3000);// 延迟3秒
AfxMessageBox("使用事件内核对象,线程正在运行!");
}
}
AfxMessageBox("使用事件内核对象,线程终止运行!");
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View
IMPLEMENT_DYNCREATE(CEx15_2View, CView)
BEGIN_MESSAGE_MAP(CEx15_2View, CView)
//{{AFX_MSG_MAP(CEx15_2View)
ON_COMMAND(ID_USE_CREATETHREAD, OnUseCreatethread)
ON_COMMAND(ID_USE_AFXBEDIGTHREAD, OnUseAfxbedigthread)
ON_COMMAND(ID_SET_START_PRIORITY, OnSetStartPriority)
ON_COMMAND(ID_SET_AFTERRUN_PRIORITY, OnSetAfterrunPriority)
ON_COMMAND(ID_COMMUNICATE_GLOBAL_START, OnCommunicateGlobalStart)
ON_COMMAND(ID_COMMUNICATE_GLOBAL_END, OnCommunicateGlobalEnd)
ON_COMMAND(ID_COMMUNICATE_USE_MESSAGE, OnCommunicateUseMessage)
ON_COMMAND(ID_COMMUNICATE_EVENT_START, OnCommunicateEventStart)
ON_COMMAND(ID_COMMUNICATE_EVENT_END, OnCommunicateEventEnd)
//}}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_USER_MSG, OnUseMessage)//手工添加
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View construction/destruction
CEx15_2View::CEx15_2View()
{
// TODO: add construction code here
}
CEx15_2View::~CEx15_2View()
{
}
BOOL CEx15_2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View drawing
void CEx15_2View::OnDraw(CDC* pDC)
{
CEx15_2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View printing
BOOL CEx15_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx15_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx15_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View diagnostics
#ifdef _DEBUG
void CEx15_2View::AssertValid() const
{
CView::AssertValid();
}
void CEx15_2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx15_2Doc* CEx15_2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx15_2Doc)));
return (CEx15_2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx15_2View message handlers
//创建线程
void CEx15_2View::OnUseAfxbedigthread() //使用Afxbe