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

📄 threadtestview.cpp

📁 《Visual C++ 6.0实例教程》配套代码
💻 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       iArray2[10];
int       iArray3[10];
int       iArray4[10];
int       iArray5[10];
int       iArray6[10];
int       iArray7[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,"写线程为数组设置的值是: ");
	int len = strlen(str);
	wsprintf(&str[len], "%d ", intSetNumber);
	pDC->TextOut(30,30,str);

	strcpy(str,"读线程 1 读到的值是: ");
	for (int i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray1[i]);
	}
	pDC->TextOut(30,50,str);

	strcpy(str,"读线程 2 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray2[i]);
	}
	pDC->TextOut(30,70,str);

	strcpy(str,"读线程 3 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray3[i]);
	}
	pDC->TextOut(30,90,str);

	strcpy(str,"读线程 4 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray4[i]);
	}
	pDC->TextOut(30,110,str);

	strcpy(str,"读线程 5 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray5[i]);
	}
	pDC->TextOut(30,130,str);

	strcpy(str,"读线程 6 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray6[i]);
	}
	pDC->TextOut(30,150,str);

	strcpy(str,"读线程 7 读到的值是: ");
	for (i=0; i<10; ++i)
	{
		int len = strlen(str);
		wsprintf(&str[len], "%d ", iArray7[i]);
	}
	pDC->TextOut(30,170,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<=1000; i++)
	{
		intSetNumber = i;
		intArray.SetArray(i);
		pView->Invalidate();
		::Sleep(1000);
	}
	return 0;
}

UINT ReadThreadProc1(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;
}

UINT ReadThreadProc2(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
		intArray.GetArray(iArray2);
		pView->Invalidate();
	}
	return 0;
}

UINT ReadThreadProc3(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
		intArray.GetArray(iArray3);
		pView->Invalidate();
	}
	return 0;
}

UINT ReadThreadProc4(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
		intArray.GetArray(iArray4);
		pView->Invalidate();
	}
	return 0;
}

UINT ReadThreadProc5(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
	intArray.GetArray(iArray5);
	pView->Invalidate();
	}
	return 0;
}

UINT ReadThreadProc6(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
	intArray.GetArray(iArray6);
	pView->Invalidate();
	}
	return 0;
}

UINT ReadThreadProc7(LPVOID param)
{
	CThreadTestApp *pApp=(CThreadTestApp *) AfxGetApp();
	CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
	CThreadTestView *pView = (CThreadTestView *) pMainFrame->GetActiveView();
	while(1)
	{
	intArray.GetArray(iArray7);
	pView->Invalidate();

	}
	return 0;
}

void CThreadTestView::OnThreadStart() 
{
	// TODO: Add your command handler code here
	HWND hWnd = GetSafeHwnd();
	AfxBeginThread(WriteThreadProc, hWnd);
	AfxBeginThread(ReadThreadProc1, hWnd);
	AfxBeginThread(ReadThreadProc2, hWnd);
	AfxBeginThread(ReadThreadProc3, hWnd);
	AfxBeginThread(ReadThreadProc4, hWnd);
	AfxBeginThread(ReadThreadProc5, hWnd);
	AfxBeginThread(ReadThreadProc6, hWnd);
	AfxBeginThread(ReadThreadProc7, hWnd);
	
}

⌨️ 快捷键说明

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