mfc中与程序相关的多线程问题 [英] problem in program related Multithreading in mfc

查看:84
本文介绍了mfc中与程序相关的多线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个Thrad过程函数ThreadProc制作了一个视图文件.编译时会出现如下错误:

ThreadDemoView.cpp(114):错误C2146:语法错误:丢失;在标识符``ThreadProc''之前;
ThreadDemoView.cpp(114):错误C2501:UNIT; :缺少存储类或类型说明符
严重错误C1004:发现文件意外结束
执行cl.exe时出错.

我是MFC的新手,请帮忙.








I have made one view file with one Thrad procedure function ThreadProc. While compiling it gives error as:

ThreadDemoView.cpp(114) : error C2146: syntax error : missing ; before identifier ''ThreadProc'';
ThreadDemoView.cpp(114) : error C2501: UNIT; : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found
Error executing cl.exe.

I am new in mfc please help.








// ThreadDemoView.cpp : implementation of the CThreadDemoView class
//

#include "stdafx.h"
#include "ThreadDemo.h"

#include "ThreadDemoDoc.h"
#include "ThreadDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView

IMPLEMENT_DYNCREATE(CThreadDemoView, CView)

BEGIN_MESSAGE_MAP(CThreadDemoView, CView)
	//{{AFX_MSG_MAP(CThreadDemoView)
	ON_WM_LBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView construction/destruction

CThreadDemoView::CThreadDemoView()
{
	// TODO: add construction code here

}

CThreadDemoView::~CThreadDemoView()
{
}

BOOL CThreadDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView drawing

void CThreadDemoView::OnDraw(CDC* pDC)
{
	CThreadDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView printing

BOOL CThreadDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CThreadDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CThreadDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView diagnostics

#ifdef _DEBUG
void CThreadDemoView::AssertValid() const
{
	CView::AssertValid();
}

void CThreadDemoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CThreadDemoDoc* CThreadDemoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadDemoDoc)));
	return (CThreadDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CThreadDemoView message handlers

struct ThreadStruct
{
	CPoint pt;
	CDC dc;
	HWND hWnd;
};
ThreadStruct structure;

UINT ThreadProc(LPVOID param);
UNIT ThreadProc(LPVOID param)
{
	structure *s=(structure *)param;
	HDC dc=GetDC(s->hWnd);
	RECT rect;
	HBrush hbr;
	hbr = CreateSolidBrush(RGB(255, 0, 0));
	SelectObject(dc,hbr);
	for(i=0;i<10;i++)
	{
		Ellipse(dc,s->pt.x,s->pt.y,s->pt.x+30,s->pt.y+30);
		Sleep(1000);
	}
	return 0;
}
void CThreadDemoView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	structure.pt=point;
	structure.hWnd=GetSafeHwnd();
	AfxBeginThread(ThreadProc,&point);
	CView::OnLButtonDown(nFlags, &structure.pt);
}

推荐答案

您在ThreadProc函数前面将UINT拼写为UNIT.
You misspelled UINT as UNIT in front of the ThreadProc function.


您还需要确保您释放您使用的所有GDI资源.

线程正在调用GetDC()哪里是ReleaseDC()调用?
在哪里删除创建的笔刷对象?
You also need to make sure you free any GDI resources you use.

the thread is calling GetDC() where is the ReleaseDC() call?
Where do you delete the brush object you create?


这篇关于mfc中与程序相关的多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆