ThreadTestView.cpp
上传用户:hvpower
上传日期:2022-07-01
资源大小:2660k
文件大小:4k
- // ThreadTestView.cpp : implementation of the CThreadTestView class
- //
- #include "stdafx.h"
- #include "afxmt.h"
- #include "ThreadTest.h"
- #include "ThreadTestDoc.h"
- #include "ThreadTestView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CThreadTestView
- CString m_Time;
- CString m_Title;
- CEvent ThreadBegin;
- CEvent ThreadEnd;
- IMPLEMENT_DYNCREATE(CThreadTestView, CView)
- BEGIN_MESSAGE_MAP(CThreadTestView, CView)
- //{{AFX_MSG_MAP(CThreadTestView)
- ON_WM_CREATE()
- ON_COMMAND(ID_THREAD_BEGIN, OnThreadBegin)
- ON_UPDATE_COMMAND_UI(ID_THREAD_BEGIN, OnUpdateThreadBegin)
- ON_COMMAND(ID_THREAD_KILL, OnThreadKill)
- ON_UPDATE_COMMAND_UI(ID_THREAD_KILL, OnUpdateThreadKill)
- //}}AFX_MSG_MAP
- ON_MESSAGE(MY_MSG01,OnMyMsg01)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CThreadTestView construction/destruction
- CThreadTestView::CThreadTestView()
- {
- // TODO: add construction code here
- }
- CThreadTestView::~CThreadTestView()
- {
- }
- BOOL CThreadTestView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CThreadTestView drawing
- void CThreadTestView::OnDraw(CDC* pDC)
- {
- CThreadTestDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- CRect rect(20,20,250,250);
- CRect rect1(20,50,250,250);
- pDC->SetTextColor(RGB(255,0,0));
- pDC->DrawText(m_Title,rect,DT_CENTER);
- pDC->SetTextColor(RGB(0,0,255));
- pDC->DrawText(m_Time,rect1,DT_CENTER);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CThreadTestView diagnostics
- #ifdef _DEBUG
- void CThreadTestView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CThreadTestView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CThreadTestDoc* CThreadTestView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadTestDoc)));
- return (CThreadTestDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CThreadTestView message handlers
- UINT ThreadProc(LPVOID param)
- {
- COleDateTime Now;
- CString ChangeTime;
- ::WaitForSingleObject(ThreadBegin.m_hObject,INFINITE);
- m_Title=L"The Thread is started... ";
- ::PostMessage((HWND)param,MY_MSG01,0,0);
- BOOL KeepRunning=true;
- while(KeepRunning)
- {
- Now=COleDateTime::GetCurrentTime();
- ChangeTime.Format(L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
- Now.GetYear(),
- Now.GetMonth(),
- Now.GetDay(),
- Now.GetHour() ,
- Now.GetMinute(),
- Now.GetSecond());
-
- m_Time=ChangeTime;
- ::PostMessage((HWND)param,MY_MSG01,0,0);
- int Result=::WaitForSingleObject(ThreadEnd.m_hObject,0);
- if (Result==WAIT_OBJECT_0)
- KeepRunning=false;
- else
- Sleep(1000);
- }
- m_Title=L"The Thread is Ended... ";
- ::PostMessage((HWND)param,MY_MSG01,0,0);
- return 0;
- }
- int CThreadTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- HWND hWnd=GetSafeHwnd();
- //AfxBeginThread(ThreadProc,hWnd);
- return 0;
- }
- void CThreadTestView::OnThreadBegin()
- {
- HWND hWnd=GetSafeHwnd();
- AfxBeginThread(ThreadProc,hWnd);
- ThreadBegin.SetEvent();
- m_Begin=!m_Begin;
-
- }
- void CThreadTestView::OnUpdateThreadBegin(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_Begin);
-
- }
- void CThreadTestView::OnThreadKill()
- {
- ThreadEnd.SetEvent();
- m_End=!m_End;
- }
- void CThreadTestView::OnUpdateThreadKill(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_End);
-
- }
- void CThreadTestView::OnMyMsg01(WPARAM wParam,LPARAM lParam)
- {
- Invalidate();
- return;
- }