我把 MFC 程序的逻辑放在哪里?

发布于 2024-09-01 12:33:01 字数 541 浏览 2 评论 0原文

我用 C++ 创建了一个应用程序核心,并将其编译到 Visual Studio 中的静态库中。我现在正在为它编写一个 GUI。 我正在使用 MFC 来执行此操作。我想出了如何映射按钮按下来执行我的应用程序核心主类的某些方法(即启动和停止的按钮)。 然而,核心类应该始终每隔一两秒从外部源采样数据。 GUI 应在每次采样后填充一些字段。我似乎无法在我的 MFC 对象(如 CDialog)中找到一个位置,我可以不断检查我的类是否已获取数据..然后它是否已将该数据放入某些文本框中。

一位朋友建议我在 OnInit() 例程上创建一个线程来处理这个问题,但该解决方案并不真正适合我。

是否没有地方可以放置一个一直被调用直到程序退出的 if 语句?

IE

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

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-09-08 12:33:01

您提到“每隔一两秒”,而另一个答案建议“使用事件驱动范例”。如何在对话框中设置计时器,并在计时器触发时从外部源采样数据。您表示您已经弄清楚如何映射按钮的事件处理程序,因此将处理程序映射到计时器应该是您的自然扩展。

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.

随梦而飞# 2024-09-08 12:33:01

我想你可以把它放在 OnIdle 中。

您最好使用事件驱动范例,尽管轮询系统会过度消耗 CPU 功率。因此,您有一个线程位于 WaitForSingleObject。设置 dataSampleReady 后,您所需要做的就是触发线程正在等待的事件。这样您就不会持续消耗 CPU 电量来检查某些内容。它将把时间交给其他进程和线程,直到需要为止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文