CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Jul 2001
    Posts
    502

    thanks john

    Now i got you! , sorry , i didnt understand you before.

    Can't i make a thread that loop all the time and waits for data ( for example : always ready to receive data from Com1 )? how do i make idle time inside the loop ?

  2. #17
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    I'm loathe to suggest this but a "quick and dirty" way is to use something like "Sleep(60)" at the beginning or end of the loop (you can vary the actual figure to suit yourself). It's a dirty way out because never-ending loops are generally considered a bad thing to use in Windows programming. I don't know what exactly you're trying to do but I'll bet there's a better way to do it than running it in a loop for the entire duration of the program.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #18
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  4. #19
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...I skimmed through the previous replies, however, if you are using a simple while loop in your thread it most-likely will show the 100% CPU disregarding how many things you are doing in between.

    For example:
    Code:
    UINT ThreadFunc(LPVOID *pvParam)
    {
      while(true)
      {
        // Do many calculations
      }
    
      return 0;
    }
    This might still give you the 100% CPU...in order to avoid that, simply force a context switch...
    Code:
    UINT ThreadFunc(LPVOID *pvParam)
    {
      while(true)
      {
        // Do many calculations
        ::Sleep(0);
      }
    
      return 0;
    }

  5. #20
    Join Date
    Feb 2004
    Posts
    21
    warning: my english is not very good, so if I've made mistakes sorry!

    Well, it depends on what you want to do with your program. I will suggest you that: leave your main function in an infinite loop, and procces other functions in separate threads. Something like this:

    // I will use _beginthread() function
    /*global*/ int repeat = 1;

    void proccesSomething()
    {
    do {
    Sleep(500);
    }while(repeat);
    }

    void main()
    {
    // do something(for example listening to port)
    _beginthread(listenFunc, 0, /*data*/);

    proccesSomething();

    // clean up
    }

    void MyFunc()
    {
    //do something
    //_beginthread(Myfunc2, 0, /*data*/);
    // and so on

    _endthread();
    }

    void listenFunc()
    {
    //do something(loop for example)
    if(/*variable == 1*/)
    {
    _beginthread(MyFunc, 0, /*data*/);
    }

    //continue listening
    _beginthread(listenFunc, 0, /*data*/);

    _endthread();
    }

    I hope that will help you!
    Last edited by defiler_z; March 2nd, 2004 at 12:07 PM.

  6. #21
    Join Date
    Mar 2004
    Posts
    7
    This will solve your problem:

    http://msdn.microsoft.com/library/de...processing.asp

    I had the same problem with an unresponsive GUI - putting the same message dispatch loop in the middle of my long process fixed the problem.

  7. #22
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: thanks!

    Originally posted by asaf a
    i didn't make myself clear enough , sorry.

    when the thread function is empty i dont have that problem, when i add the "While (TRUE)" loop inside the thread function the cpu jumps to 100% ( even when the while loop is empty of code ).
    sry ... I tried to say that you should use a timer instead ... but forget it ...

  8. #23
    Join Date
    Sep 1999
    Posts
    46
    hi

    i am simpleman

    1. the easyest way : Using Sleep(1);
    i think the Time in Sleep() mentioned above is too much.

    2. more hard way : Using Event
    i think you receive some data from socket, if then make an event for the each socket and using WaitForSingleObject() or WaitForMultipleObjects(). i think you can get the network event if you use WSAEnumNetworkEvents().

    Good luck to you ..

  9. #24
    Join Date
    Jul 2001
    Posts
    502

    Thank you all !

    Thank you for all your help , i solved the problem.

    simpleman :

    Thanks , as for now i'm using the solution you suggested, i put Sleep(1) in the loop and the CPU usage decreased from 100% to 3% in full calculations inside loop. thanks.

    John E and Andreas Masur : thanks.


    smg123 : thank you for the link , now i know that there is idle processing and i will implement it in my program in the future.

    Thanks you all!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured