MFC MDI中的多线程 [英] Multithreading in MFC MDI

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

问题描述



我试图从在CMyView.cpp中启动的线程访问在解决方案的MainFrame中声明的全局变量.如何在CMyView.h的结构 THREADVIEW 中没有 CMainFrame * _MainFrame; 的情况下访问这些参数,如下所示?有什么想法吗?

Hi,

I am trying to access globally variables declared in MainFrame of my solution from a thread that starts in my CMyView.cpp. How can I access these parameters without the CMainFrame * _MainFrame; in my structure THREADVIEW in CMyView.h, as below? Any ideas?

typedef struct THREADVIEW{
    * _this;

   //CMainFrame * _MainFrame; /* I cannot have this here */
 
} THREADVIEW;



非常感谢,

雅尔达斯


OP的添加:

首先,非常感谢您的即时评论.这是更详细的内容.



Many thanks,

Yoldas


addition by the OP:
Hi,
First of all many thanks for your immediate comments. Here are the things in more detail.

///////////////In my view.cpp:
void CView::OnSprint()
{
	CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd();		
.
.
. 	/* kick off thread */
THREADVIEW *_pview = new THREADVIEW;
_pview->_this = this;
AfxBeginThread(ChangeColorThread, _pview);

}
UINT CView::ChangeColorThread(LPVOID param)
{
	THREADVIEW * thd = (THREADVIEW*)param;
	thd->_this->StateOnThread();

	AfxEndThread(1, FALSE); 

	return 1;
}

void CView:: StateOnThread ()
{
	CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd();	
	int a = pFrame->AA;
	/* I want to return AA from main frame but I cannot because I cannot declare CMainFrame * pFrame; in my THREADVIEW */
}

///////////// in my view.h

public:

	typedef struct THREADVIEW{
		CView * _this;
		// cannot have CMainFrame * pFrame;
	} THREADVIEW;


	void StateOnThread();

	static UINT ChangeColorThread(LPVOID param);


////////////my mainframe has my in AA:

class CMainFrame : public CMDIFrameWnd
{
	DECLARE_DYNAMIC(CMainFrame)
public:
int AA;
}



我正在尝试访问.在显示子框架之后,我的线程从工具栏按钮开始,但是我看不到为什么这与为什么我无法访问Cmainframe参数有关.

非常感谢您的投入.

此致

Yoldas



which I am trying to access. My thread kicks off from toolbar button after the child frame is displayed but I do not see why this has anything to do with why I cannot access Cmainframe parameters.

I greatly appreciate your input.

Regards,

Yoldas

推荐答案

您可以在此处访问它,但这不是一个好主意,因为您需要锁定MainFrame,这会变得肮脏而复杂. br/>
我建议您创建一个单例类来处理您的全局变量.保持成员变量的安全,并为数据提供线程安全的访问方法.

这样的事情.

头文件
You could access it there, but it''s a bad idea as you''d need to lock the MainFrame and that can get dirty and complex.

I recommend that you create a singleton class to handle your globals. Keep the member variables protected and provide thread safe access methods for the data.

Something like this.

Header File
class CWorkSpace;

extern CWorkSpace WorkSpace;

class CWorkSpace
{
protected:
   THREADVIEW ThreadView;

public:
   THREADVIEW GetThreadView(void) const
   {
      CLock Lock;
      return ThreadView;
   }

   void SetThreadView(const THREADVIEW & NewThreadView)
   {
      CLock Lock;

      ThreadView = NewThreadView;
   }
}



CPP文件



CPP File

#include "WorkSpace.h"

CWorkSpace WorkSpace;



您需要替换CLock Lock;



You''ll need to substitute the CLock Lock; code with whatever locking mechanism you choose.


如果变量是全局变量,则您可以访问它们(只需在线程源文件中将它们声明为extern).
If the variables are global then you can access them (simply declare them extern in your thread source file).


1.您现在有内存泄漏,请修复:
1. You have a memory leak now, fix:
// cpp:
void CYourView::OnSprint()
{
  //..
  THREADVIEW sPar = { this }; // on-stack instance, will be automatically "deleted"
  AfxBeginThread(ChangeColorThread, &sPar); // note: you could pass this directly as well
  //..
}


2.您的线程参数可以包含CMainFrame-pointer:


2. Your thread parameter could contain the CMainFrame-pointer:

// cpp:
typedef struct {
  CYourView* pView;
  CMainFrame* pFrame;
} THREADVIEW;

void CYourView::OnSprint()
{
  //..
  THREADVIEW sPar = { this, static_cast<CMainFrame*>(::AfxGetMainWnd()) };
  AfxBeginThread(ChangeColorThread, &sPar);
  //..
}


3.您的StateOnThread可能返回值或什至是框架变量的引用:


3. Your StateOnThread could return the value or even the reference of the frame variable:

// cpp:
int /*int&*/ CYourView::StateOnThread()
{
  CMainFrame* pcFrame(static_cast<CMainFrame*>(AfxGetMainWnd()));
  return pcFrame ? pcFrame->AA : 0;
  /*
  static int si(0);
  return pcFrame ? pcFrame->AA : si;
  */
}


4.对于更长的类型(如INT_PTR:)


4. Do not use this kind of access for the types longer as INT_PTR :)


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

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