ThreadTestView.cpp
上传用户:hvpower
上传日期:2022-07-01
资源大小:2660k
文件大小:4k
源码类别:

进程与线程

开发平台:

Visual C++

  1. // ThreadTestView.cpp : implementation of the CThreadTestView class
  2. //
  3. #include "stdafx.h"
  4. #include "afxmt.h"
  5. #include "ThreadTest.h"
  6. #include "ThreadTestDoc.h"
  7. #include "ThreadTestView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CThreadTestView
  15. CString m_Time;
  16. CString m_Title;
  17. CEvent ThreadBegin;
  18. CEvent ThreadEnd;
  19. IMPLEMENT_DYNCREATE(CThreadTestView, CView)
  20. BEGIN_MESSAGE_MAP(CThreadTestView, CView)
  21. //{{AFX_MSG_MAP(CThreadTestView)
  22. ON_WM_CREATE()
  23. ON_COMMAND(ID_THREAD_BEGIN, OnThreadBegin)
  24. ON_UPDATE_COMMAND_UI(ID_THREAD_BEGIN, OnUpdateThreadBegin)
  25. ON_COMMAND(ID_THREAD_KILL, OnThreadKill)
  26. ON_UPDATE_COMMAND_UI(ID_THREAD_KILL, OnUpdateThreadKill)
  27. //}}AFX_MSG_MAP
  28. ON_MESSAGE(MY_MSG01,OnMyMsg01)
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CThreadTestView construction/destruction
  32. CThreadTestView::CThreadTestView()
  33. {
  34. // TODO: add construction code here
  35. }
  36. CThreadTestView::~CThreadTestView()
  37. {
  38. }
  39. BOOL CThreadTestView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41. // TODO: Modify the Window class or styles here by modifying
  42. //  the CREATESTRUCT cs
  43. return CView::PreCreateWindow(cs);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CThreadTestView drawing
  47. void CThreadTestView::OnDraw(CDC* pDC)
  48. {
  49. CThreadTestDoc* pDoc = GetDocument();
  50. ASSERT_VALID(pDoc);
  51. CRect rect(20,20,250,250);
  52. CRect rect1(20,50,250,250);
  53. pDC->SetTextColor(RGB(255,0,0));
  54. pDC->DrawText(m_Title,rect,DT_CENTER);
  55. pDC->SetTextColor(RGB(0,0,255));
  56. pDC->DrawText(m_Time,rect1,DT_CENTER);
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CThreadTestView diagnostics
  60. #ifdef _DEBUG
  61. void CThreadTestView::AssertValid() const
  62. {
  63. CView::AssertValid();
  64. }
  65. void CThreadTestView::Dump(CDumpContext& dc) const
  66. {
  67. CView::Dump(dc);
  68. }
  69. CThreadTestDoc* CThreadTestView::GetDocument() // non-debug version is inline
  70. {
  71. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadTestDoc)));
  72. return (CThreadTestDoc*)m_pDocument;
  73. }
  74. #endif //_DEBUG
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CThreadTestView message handlers
  77. UINT ThreadProc(LPVOID param)
  78. {
  79. COleDateTime Now;
  80. CString ChangeTime;
  81. ::WaitForSingleObject(ThreadBegin.m_hObject,INFINITE);
  82. m_Title=L"The Thread is started...                ";
  83. ::PostMessage((HWND)param,MY_MSG01,0,0);
  84. BOOL KeepRunning=true;
  85. while(KeepRunning)
  86. {
  87. Now=COleDateTime::GetCurrentTime();
  88. ChangeTime.Format(L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d", 
  89. Now.GetYear(),
  90. Now.GetMonth(),
  91. Now.GetDay(),
  92. Now.GetHour() ,
  93. Now.GetMinute(),
  94. Now.GetSecond());
  95. m_Time=ChangeTime;
  96. ::PostMessage((HWND)param,MY_MSG01,0,0);
  97. int Result=::WaitForSingleObject(ThreadEnd.m_hObject,0);
  98. if (Result==WAIT_OBJECT_0)
  99. KeepRunning=false;
  100. else
  101. Sleep(1000);
  102. }
  103. m_Title=L"The Thread is Ended...                 ";
  104. ::PostMessage((HWND)param,MY_MSG01,0,0);
  105. return 0;
  106. }
  107. int CThreadTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  108. {
  109. if (CView::OnCreate(lpCreateStruct) == -1)
  110. return -1;
  111. HWND hWnd=GetSafeHwnd();
  112. //AfxBeginThread(ThreadProc,hWnd);
  113. return 0;
  114. }
  115. void CThreadTestView::OnThreadBegin() 
  116. {
  117. HWND hWnd=GetSafeHwnd();
  118. AfxBeginThread(ThreadProc,hWnd);
  119. ThreadBegin.SetEvent();
  120. m_Begin=!m_Begin;
  121. }
  122. void CThreadTestView::OnUpdateThreadBegin(CCmdUI* pCmdUI) 
  123. {
  124. pCmdUI->SetCheck(m_Begin);
  125. }
  126. void CThreadTestView::OnThreadKill() 
  127. {
  128. ThreadEnd.SetEvent();
  129. m_End=!m_End;
  130. }
  131. void CThreadTestView::OnUpdateThreadKill(CCmdUI* pCmdUI) 
  132. {
  133. pCmdUI->SetCheck(m_End);
  134. }
  135. void CThreadTestView::OnMyMsg01(WPARAM wParam,LPARAM lParam)
  136. {
  137. Invalidate();
  138. return;
  139. }