tags:

views:

83

answers:

2

I created an application core, in C++, that I've compiled into a static library in Visual Studio. I am now at the process of writing a GUI for it. I am using MFC to do this. I figured out how to map button presses to execute certain methods of my application core's main class (i.e. buttons to have it start and stop). The core class however, should always be sampling data from an external source every second or two. The GUI should then populate some fields after each sample is taken. I can't seem to find a spot in my MFC objects like CDialog that I can constantly check to see if my class has grabbed the data.. then if it has put that data into some of the text boxes.

A friend suggested that I create a thread on the OnInit() routine that would take care of this, but that solution isn't really working for me.

Is there no spot where I can put an if statement that keeps being called until the program quits?

i.e.

if( coreapp.dataSampleReady() ) {
  // put coreapp.dataItem1() in TextBox1
  // set progress bar to coreapp.dataItem2()
  // etc.
  // reset dataSampleReady
}
+1  A: 

I guess you could put it in OnIdle.

You are better off using an event driven paradigm though as a polling system will suck CPU power quite excessively. Therefore you have a thread that sits in a WaitForSingleObject. When dataSampleReady is set all you need to do is trigger the event that the thread is waiting for. This way you aren't continually sucking CPU power to check something. It will sit giving its time up to other processes and threads until its needed.

Goz
The problem with OnIdle, is that my appcore class is a member of my CDialog class, which is a member of the CWinApp class.Is my solution then to make the constructor of the CDialog take a pointer to my coreapp (then save it as a member), and create my coreapp class in th CWinApp class?
Matthew
Surely the OnIdle can call accessor members on the dialog then? Don't forget you can ALWAYS get at your CWinApp derived class through AfxGetApp()
Goz
+1  A: 

You mentioned "every second or two" while another answer suggested "using an event driven paradigm". How about setting a Timer in the dialog and when the timer fires, sample data from your external source. You indicated you figured out how to map event handlers for buttons, so mapping a handler to the timer should be a natural extension for you.

SAMills
I figured this out after a while, so this is what I used as my solution. As my dataSampleReady() underneath is just polling the time from the clock and comparing it to 'time to poll'.
Matthew