C ++ MultiThreading with visual studio express 2010 Forms Application [英] C++ MultiThreading with visual studio express 2010 Forms Application

查看:187
本文介绍了C ++ MultiThreading with visual studio express 2010 Forms Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的Windows窗体应用程序连接到一块硬件,获取大量的数据(〜1 GSample /秒),处理它,并将其吐出到屏幕上点击按钮。我现在试图在一个循环自动化的过程,可以在任何时间开始/停止,所以我可以监视它,同时调整输入到采集硬件。我认为很清楚,我需要在一个单独的线程,但我有一个时间试图这样做在c ++ / cli - 我发现了一些很好的例子使用MFC,这是不支持的Express。



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

  void Form1 :: realTimeUpdate()
{
// live is一个布尔变量通过形式
上的按钮设置while(live)
{
displayVariance(getVar(getQuadratures(100),nbrSamples));
}
}



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



有什么建议我怎么去关于在单独的线程上运行此代码?



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


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


  2. 问题:编译器不会让我声明一个全局的System :: Threading.Thread

  3. $(在Java中声明一个全局线程并启动/停止它) b $ b
  4. 这个美丽的例子。问题:需要MFC


任何建议都将非常感激。

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

这是一个 C ++ / CLI中的线程教程


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!

解决方案

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.

这篇关于C ++ MultiThreading with visual studio express 2010 Forms Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆