简体   繁体   中英

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.

Please Sugest/guide me how to do this..

Thanks.

Move the while loop into the thread functions:

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. 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. 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/..). 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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