简体   繁体   English

如何在无限循环中运行线程

[英]How to Run a Thread in an Infinite loop

In one of my Project i need to run two operations( Open & Close Operations ) at a same time(ie, parallel/simultaneously). 在我的一个项目中,我需要同时运行两个操作(打开和关闭操作)(即并行/同时)。 I've created two threads for respective Operation like this: 我为各自的操作创建了两个线程,如下所示:

UINT MyThread1(LPVOID lParam)
{
 // code for Open Operation..
 // other stuffs...

 return(1);
}

UINT MyThread2(LPVOID lParam)
{
 //Code for Close Operation..
 //Other Stuffs..

return(1);
}

void CMyProject : OnbnClickedOpen()
{
   // Here am running OPen Operation Continously..
 while(1) {
 AfxBeginThread(MyThread1,0);
 }
 }

void CMyProject : onbnClickedClose()
{
  //Here am running Close Operation continously...
  while(1) {
  AfxBeginThread(MyThread2,0);
 }
}

Here am not able to run the threads continuously. 这里无法连续运行线程。 I've tried an alternative way like this: 我尝试过这样的替代方法:

void CMyProject : OnbnClickedOpen()
{
   while( 1 )
   {
     //Code for Open Operation..
   }
 }

void CMyProject : onbnClickedClose()
{
  while(1)
  {
    //Code for Close Operation..
  }
}

With this i can run either Open Operation Continuously or close operation Continuously but not both at at time. 有了这个,我可以连续运行Open Operation或者连续运行,但不能同时运行。

Please Sugest/guide me how to do this.. 请Sugest /指导我如何做到这一点..

Thanks. 谢谢。

Move the while loop into the thread functions: while循环移动到线程函数中:

UINT MyThread1(LPVOID lParam)
{
 // Here am running OPen Operation Continously..
 while(1) {
   // code for Open Operation..
   // other stuffs...
 }

 return(1);
}

UINT MyThread2(LPVOID lParam)
{
  //Here am running Close Operation continously...
  while(1) {
   //Code for Close Operation..
   //Other Stuffs..
  }

return(1);
}

void CMyProject : OnbnClickedOpen()
{
 AfxBeginThread(MyThread1,0);
}

void CMyProject : onbnClickedClose()
{
  AfxBeginThread(MyThread2,0);
}

The problem is that you are calling the infinite while(1) loop within the main thread itself, not within these separate threads. 问题是你在主线程本身内调用无限while(1)循环,而不是在这些单独的线程中。 So as soon as you enter this infinite while on the main thread, it will keep running it and not execute any other code. 因此,只要在主线程上输入此无限时,它将继续运行它而不执行任何其他代码。

You should move your infinite loops into their respective threads, wrapping the entire code there in the while loop will allow it to run infinitely, but separate from the main thread. 你应该将无限循环移动到它们各自的线程中,将整个代码包装在while循环中将允许它无限运行,但是与主线程分开。 Therefor the execution of the main thread will never be blocked. 因此,永远不会阻止主线程的执行。

:*D, you miss it, an endless loop can hook up use of your processor and consume your computer resources (RAM/mem/..). :* D,你错过了,无限循环可以连接你的处理器使用你的计算机资源(RAM / mem / ..)。 a background worker is to be designed for some particular long-time consuming task that does its job like an endless loop. 后台工作者将被设计用于某些特定的长时间消耗任务,其工作就像无限循环一样。 Beacuse it continues its job all the time, your computer 's eg PnP devices may fail to obtain their stands in your computer; 因为它一直在继续工作,你的计算机,例如PnP设备可能无法在你的计算机上获得它们的支架; they get smacked down, and your computer sometimes freezes itself until the running task finishes its job. 他们被打倒了,你的计算机有时会自行冻结,直到正在运行的任务完成它的工作。 So the background thread must contain the while loop not the while loop that is to contain the background worker. 因此后台线程必须包含while循环而不是包含后台worker的while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM