C++使用 Visual Studio Express 2010 表单应用程序进行多线程处理

发布于 2024-11-02 03:46:17 字数 1101 浏览 0 评论 0原文

我正在开发一个 Windows 窗体应用程序,它连接到一个硬件,获取大量数据(~1 GSample/秒),对其进行处理,然后单击按钮将其输出到屏幕上。我现在尝试在一个可以随时启动/停止的循环中自动化该过程,以便我可以在调整采集硬件的输入的同时对其进行监控。我认为很明显我需要在一个单独的线程上执行此操作,但我在 c++/cli 中尝试执行此操作很费时间 - 我发现了许多使用 MFC 的好示例,但 MFC 不支持表达。

具体来说:我的任务是按下 Form1.h 中处理的按钮,调用主文件 Acquisition.cpp 中的函数,其中包含以下代码(当前是无限循环)

void Form1::realTimeUpdate()  
{  
    // live is a boolean variable set by a button on the form
    while(live)  
    {  
        displayVariance(getVar(getQuadratures(100),nbrSamples));  
    }  
}  

我希望在单独的线程中执行此代码,以便主程序可以监听用户停止操作的请求。如果没有线程,我目前必须强制退出程序(或将其设置为运行固定次数)才能停止它。

有什么建议我可以如何在单独的线程上运行此代码吗?

我已经(未成功)尝试了一些操作:

  1. 修改此 Microsoft 示例。问题:需要 /clr:oldSyntax 选项,该选项与程序中的其他 1300 行代码不兼容。

  2. 尝试做我在 Java 中所做的事情(声明一个全局线程并从代码中的任何点启动/停止它。问题:编译器不允许我声明一个全局 System::Threading.Thread

  3. 这个 问题:需要 MFC。

任何建议都会非常好赞赏!

I am developing a Windows forms application which connects to a piece of hardware, acquires a lot of data (~1 GSample/sec), processes it, and spits it out to the screen upon a button click. I am now trying to automate the process in a loop that can be started/stopped at any time so I can monitor it whilst tweaking the input to the acquisition hardware. I thinks it's clear that I need to do this on a separate thread, but I'm having a heck of a time trying to do this in c++/cli - I have found a number of good examples using MFC, which is not supported by Express.

Specifically: My task is to press a button which is handled in Form1.h, to call a function in my main file Acquisition.cpp which contains the following code (currently an infinite loop)

void Form1::realTimeUpdate()  
{  
    // live is a boolean variable set by a button on the form
    while(live)  
    {  
        displayVariance(getVar(getQuadratures(100),nbrSamples));  
    }  
}  

I wish to execute this code in a separate thread so that the main program can listen for the user request to stop the operation. Without threading, I currently have to forcefully quit the program (or set it to run a fixed number of times) to stop it.

Is there any suggestions how I might go about running this code on a separate thread?

I've (unsuccessfully) tried a few things already:

  1. Modifying the example given in This Microsoft Example. Problem: requires /clr:oldSyntax option which is incompatible with the other 1300 lines of code in the program.

  2. Trying to do what I'd do in Java (Declare a global thread and start/stop it from any point in the code. Problem: Compiler won't let me declare a global System::Threading.Thread

  3. this beautiful example. Problem: Requires MFC.

Any suggestions would be greatly appreciated!

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

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

发布评论

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

评论(2

瑾夏年华 2024-11-09 03:46:17

您可以使用BackgroundWorker 或Thread 来处理这个问题。但是,您需要确保更新 UI 的工作部分被封送回 UI 线程。

这是有关 C++/CLI 线程的教程

You can use a BackgroundWorker or a Thread to handle this. You'll need to make sure that the portion of your work that updates the UI is marshaled back to the UI thread, however.

Here is a tutorial on threading in C++/CLI.

寂寞美少年 2024-11-09 03:46:17

作为记录,根据 Reed 关于使用 BackgroundWorker 的建议,我筛选了 页面并修改了我的代码,以便:

  1. 它创建了一个新的backgroundWorker BGWorker,其中BGWorker->DoWork()调用了我的realTimeUpdate()函数。

  2. 主窗体上的按钮调用 RunWorkerAsync() 或 CancelAsync(),具体取决于进程是否正在运行(通过主程序中的布尔标志检查)。

  3. realTimeUpdate() 函数现在传递一个BackgroundWorker - realTimeUpdate(BackgroundWorker^worker, DoWorkEventArgs^e) 在内部循环中完成每个计算后,它调用worker->ReportProgress(result) ) 功能。在 BGWorker->ProgressChanged() 函数中,有一个单独的函数 upDataUI(int) 在主窗体上绘制结果。

再次感谢您的帮助。

For the record, upon Reed's suggestion about using a BackgroundWorker, I sifted through the code at the bottom of this page and modified my code so that:

  1. It created a new backgroundWorker BGWorker in which BGWorker->DoWork() called my realTimeUpdate() function.

  2. A button on the main Form calls either RunWorkerAsync() or CancelAsync() depending on whether or not the process is running (checked by a boolean flag in my main program).

  3. The realTimeUpdate() function is now passed a BackgroundWorker - realTimeUpdate(BackgroundWorker^ worker, DoWorkEventArgs ^ e) After each calculation is complete within the internal loop, it calls worker->ReportProgress(result) function. In the BGWorker->ProgressChanged() function a separate function, upDataUI(int) draws the result on the main form.

Thanks again for the help.

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