Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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;
 
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;
}
 
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
Posted 1-Nov-12 8:55am
Edited 1-Nov-12 13:59pm
Nelek63.2K
v5
Comments
Eugen Podsypalnikov at 1-Nov-12 14:52pm
   
What types of the variables and
what kind of the access (read/write) are wanted, please ? :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 File
 
#include "WorkSpace.h"

CWorkSpace WorkSpace;
 
You'll need to substitute the CLock Lock; code with whatever locking mechanism you choose.
  Permalink  
Comments
JackDingler at 1-Nov-12 16:10pm
   
The CMainFrame shouldn't be used as a miscellaneous bin to put all of your global variables in.
 
And you do not want threads directly accessing your CMainFrame. If you don't lock it properly and intercept every message path that may cause a problem, your application will suffer from mystery crashes.
 
If you enable locking, you run a risk of seeing severe performance problems in message handling and possible freezing up in the case of race conditions.
 
CMainFrame has plenty to do in maintaining your main window. Don't give it a bunch of unrelated tasks to manage. Build other classes to take on this role.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the variables are global then you can access them (simply declare them extern in your thread source file).
  Permalink  
v2
Comments
Yoldas77 at 1-Nov-12 15:49pm
   
Not sure how, would you kindly look at my question which I have just updated and has more info. Regards.
JackDingler at 1-Nov-12 16:05pm
   
You can access your CMainFrame from a thread, though it's a bad idea and you shouldn't do it.
 
You shouldn't be accessing globals at all without a locking mechanism, unless you know for a fact that they will never change.
 
CMainFrame * pMainFrame = (CMainFrame *) AfxGetMainWnd();
CPallini at 1-Nov-12 16:07pm
   
Why cannot you just call AfxGetMainWnd inside the thread?
However, beware worker threads should not 'touch the GUI' see Newcomer's essay:
http://www.flounder.com/workerthreads.htm
Yoldas77 at 1-Nov-12 16:09pm
   
Hi, as you see that's exactly what I have but I cannot access the variables. Changing their values is not an issue. I just want my code to return these parameters. Confusing me.
JackDingler at 1-Nov-12 16:41pm
   
See my comment just above to learn how to do it.
CPallini at 1-Nov-12 16:44pm
   
Why cannot?
What is the problem are you experiencing?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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. 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. 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. Do not use this kind of access for the types longer as INT_PTR Smile | :)
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100