开发者

Where do I put the logic of my MFC program?

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 h开发者_如何学Cow 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
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜