📄 threadtestview.cpp
字号:
// ThreadTestView.cpp : implementation of the CThreadTestView class
//
#include "stdafx.h"
#include "ThreadTest.h"
#include "ThreadTestDoc.h"
#include "ThreadTestView.h"
#include "MainFrm.h"
#include "afxmt.h"
#include "IntArray.h"
CIntArray intArray;
int iArray1[10];
int intSetNumber;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CThreadTestView
IMPLEMENT_DYNCREATE(CThreadTestView, CView)
BEGIN_MESSAGE_MAP(CThreadTestView, CView)
//{{AFX_MSG_MAP(CThreadTestView)
ON_COMMAND(ID_THREAD_START, OnThreadStart)
//}}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)
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);
// TODO: add draw code for native data here
char str[70];
strcpy(str,"线程 1 为数组设置的值是: ");
int len = strlen(str);
wsprintf(&str[len], "%d ", intSetNumber);
pDC->TextOut(30,30,str);
strcpy(str,"线程 2 读到的值是: ");
for (int i=0; i<10; ++i)
{
int len = strlen(str);
wsprintf(&str[len], "%d ", iArray1[i]);
}
pDC->TextOut(30,50,str);
}
/////////////////////////////////////////////////////////////////////////////
// CThreadTestView printing
BOOL CThreadTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CThreadTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CThreadTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// 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 WriteThreadProc(LPVOID param)
{
CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
for(int i=1; i<=10; i++)
{
intSetNumber = i;
intArray.SetArray(i);
pView->Invalidate();
::Sleep(2000);
}
return 0;
}
UINT ReadThreadProc(LPVOID param)
{
CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
while(1)
{
intArray.GetArray(iArray1);
pView->Invalidate();
}
return 0;
}
void CThreadTestView::OnThreadStart()
{
// TODO: Add your command handler code here
HWND hWnd = GetSafeHwnd();
AfxBeginThread(WriteThreadProc, hWnd);
AfxBeginThread(ReadThreadProc, hWnd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -