如何同时运行两个无限循环 [英] How to run two infinite loops simultaneously at a time

查看:910
本文介绍了如何同时运行两个无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone ..



我在交易平台上工作。我的情况就像我需要无限地运行两个操作(打开和关闭操作)。让我详细说明:



  void  CTradingPlatform:OnBnClickedOPEN() 
{
while 1
{
// 基于某些条件.. //这里的Open Opeartion代码...
}

}

void CTradingPlatform:OnBnClickedCLOSE()
{
while 1
{
/ / 基于某些条件..此处CLOSE操作的代码..
}
}





这里的问题是..我不能同时运行这两个循环。我只能运行其中一个。这两个while循环必须一次运行。对此有什么解决方案..请在此建议我..



谢谢大家:)

解决方案

除非循环以某种严格的方式同步,因此您可以在单个循环中编写它们,唯一的方法是在不同的线程中执行它们。因为UI也参与其中(否则你为什么要使用MFC?),即使一个循环也需要一个单独的线程。换句话说,对于两个循环,您将在现有UI线程的两个线程处添加,总共至少有三个线程。



-SA

Hello Everyone..

Am working on the Trading Platform. I''m having a situation like where i need to run two operations ( Open & Close Operations ) infinitely. let me elaborate it clearly :

void CTradingPlatform : OnBnClickedOPEN()
{
  while(1)
  {
    // Based on some conditions.. //code for the Open Opeartion here ...
  }

}

void CTradingPlatform : OnBnClickedCLOSE()
{
 while(1)
 {
  // Based on some conditions.. code for CLOSE Operation here..
 }
}



Here the problem is.. I cant run these two while loops at a time. I can run only one of them. These two while loops must run at a time. Is there any solution for this.. Please suggest me on this..

Thank you all :)

解决方案

Unless the loops are synchronized in some rigid manner, so you could write them in a single loop, the only way to do this is to execute them in different threads. As UI is also involved (otherwise why would you use MFC?), even one loop will need a separate thread. In other words, for two loops, you will add at two threads to existing UI thread, to have at least three threads in total.

—SA


这篇关于如何同时运行两个无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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