Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. loop speed

loop speed

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancetutorial
23 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    locoone
    wrote on last edited by
    #1

    is there any way to slow a loop down my program is 1 big loop and it runs my cpu at almost 100% how can i cut it down to between 50 and 75%? i would ask about making it run in the background but thats way over my head lol would like to know how to code a stop and pause button for it tho.

    C K 2 Replies Last reply
    0
    • L locoone

      is there any way to slow a loop down my program is 1 big loop and it runs my cpu at almost 100% how can i cut it down to between 50 and 75%? i would ask about making it run in the background but thats way over my head lol would like to know how to code a stop and pause button for it tho.

      C Offline
      C Offline
      CodeBeetle
      wrote on last edited by
      #2

      maybe use the Sleep(1); function. That will make the thread sleep for 1ms so that other threads can execute.


      CodeBeetle.Com


      L 1 Reply Last reply
      0
      • C CodeBeetle

        maybe use the Sleep(1); function. That will make the thread sleep for 1ms so that other threads can execute.


        CodeBeetle.Com


        L Offline
        L Offline
        locoone
        wrote on last edited by
        #3

        Sleep(1); makes the program lock up

        1 Reply Last reply
        0
        • L locoone

          is there any way to slow a loop down my program is 1 big loop and it runs my cpu at almost 100% how can i cut it down to between 50 and 75%? i would ask about making it run in the background but thats way over my head lol would like to know how to code a stop and pause button for it tho.

          K Offline
          K Offline
          khan
          wrote on last edited by
          #4

          Use a Thread for the task, and lower the priority of the thread. For Stop, signal the thread to stop, like: make the loop test variable = 0; so the loop would stop. For Pause, use SuspendThread() and ResumeThread(). this is this.

          L 1 Reply Last reply
          0
          • K khan

            Use a Thread for the task, and lower the priority of the thread. For Stop, signal the thread to stop, like: make the loop test variable = 0; so the loop would stop. For Pause, use SuspendThread() and ResumeThread(). this is this.

            L Offline
            L Offline
            locoone
            wrote on last edited by
            #5

            im a noob that is russian to me :(

            K D 2 Replies Last reply
            0
            • L locoone

              im a noob that is russian to me :(

              K Offline
              K Offline
              khan
              wrote on last edited by
              #6

              #include #include #include bool g_bWorking; void Worker() { while (g_bWorking) { //this is the loop where the program does all the work. printf("ASD"); } } unsigned long __stdcall Thread(void* lpVoid) { Worker(); return 0; } main() { unsigned long id; HANDLE h; g_bWorking = TRUE; h = CreateThread(NULL,0,Thread,0,CREATE_SUSPENDED,&id); SetThreadPriority(h,THREAD_PRIORITY_LOWEST); ResumeThread(h); getch(); SuspendThread(h); getch(); ResumeThread(h); getch(); return 0; } It will start the thread, then on keypress, pause it, then on keypress resume it. You can see the result by running this console application. When you need to stop it, make g_bWorking = false. And by-the-way, are you writing a console app, or a windows app? this is this.

              L 1 Reply Last reply
              0
              • K khan

                #include #include #include bool g_bWorking; void Worker() { while (g_bWorking) { //this is the loop where the program does all the work. printf("ASD"); } } unsigned long __stdcall Thread(void* lpVoid) { Worker(); return 0; } main() { unsigned long id; HANDLE h; g_bWorking = TRUE; h = CreateThread(NULL,0,Thread,0,CREATE_SUSPENDED,&id); SetThreadPriority(h,THREAD_PRIORITY_LOWEST); ResumeThread(h); getch(); SuspendThread(h); getch(); ResumeThread(h); getch(); return 0; } It will start the thread, then on keypress, pause it, then on keypress resume it. You can see the result by running this console application. When you need to stop it, make g_bWorking = false. And by-the-way, are you writing a console app, or a windows app? this is this.

                L Offline
                L Offline
                locoone
                wrote on last edited by
                #7

                windows app

                K 1 Reply Last reply
                0
                • L locoone

                  windows app

                  K Offline
                  K Offline
                  khan
                  wrote on last edited by
                  #8

                  Please also specify if you are using MFC or Win32. Is it a dialog-based application? And Is the worker() function a member of your class? this is this.

                  L 1 Reply Last reply
                  0
                  • K khan

                    Please also specify if you are using MFC or Win32. Is it a dialog-based application? And Is the worker() function a member of your class? this is this.

                    L Offline
                    L Offline
                    locoone
                    wrote on last edited by
                    #9

                    it is mfc and dialog and the last im not sure what ya mean

                    K 1 Reply Last reply
                    0
                    • L locoone

                      it is mfc and dialog and the last im not sure what ya mean

                      K Offline
                      K Offline
                      khan
                      wrote on last edited by
                      #10

                      I assume this is your worker function: void CMyDlg::Worker() { while (m_bWorking) { //processing here. } } In your header file: HANDLE m_hThread; bool m_bWorking; static DWORD __stdcall Thread(LPVOID lpVoid); In your .cpp file: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { CMyDlg* parent = (CMyDlg*)lpVoid; parent->Worker(); return 0; } void CMyDlg::OnButtonStart()//your button handler function. { m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); } void CMyDlg::OnButtonSuspend()//pause it { SuspendThread(m_hThread); } void CMyDlg::OnButtonResume()//resume it { ResumeThread(m_hThread); } void CMyDlg::OnButtonStop()//stop it { m_bWorking = false; } I hope that helps. this is this.

                      L D 2 Replies Last reply
                      0
                      • K khan

                        I assume this is your worker function: void CMyDlg::Worker() { while (m_bWorking) { //processing here. } } In your header file: HANDLE m_hThread; bool m_bWorking; static DWORD __stdcall Thread(LPVOID lpVoid); In your .cpp file: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { CMyDlg* parent = (CMyDlg*)lpVoid; parent->Worker(); return 0; } void CMyDlg::OnButtonStart()//your button handler function. { m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); } void CMyDlg::OnButtonSuspend()//pause it { SuspendThread(m_hThread); } void CMyDlg::OnButtonResume()//resume it { ResumeThread(m_hThread); } void CMyDlg::OnButtonStop()//stop it { m_bWorking = false; } I hope that helps. this is this.

                        L Offline
                        L Offline
                        locoone
                        wrote on last edited by
                        #11

                        m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); C:\Program Files\Microsoft Visual Studio\MyProjects\wordlist\wordlistDlg.cpp(230) : error C2065: 'id' : undeclared identifier its about time to give up

                        K 1 Reply Last reply
                        0
                        • L locoone

                          m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); C:\Program Files\Microsoft Visual Studio\MyProjects\wordlist\wordlistDlg.cpp(230) : error C2065: 'id' : undeclared identifier its about time to give up

                          K Offline
                          K Offline
                          khan
                          wrote on last edited by
                          #12

                          You must have seen in my first post that: unsigned long id; or DWORD id; Please have a look at this MSDN url also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp[^] this is this.

                          L 1 Reply Last reply
                          0
                          • K khan

                            You must have seen in my first post that: unsigned long id; or DWORD id; Please have a look at this MSDN url also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp[^] this is this.

                            L Offline
                            L Offline
                            locoone
                            wrote on last edited by
                            #13

                            unsigned long id; or DWORD id; either of them will give another error if i try them and i cant even come close of making since of the web page :mad: just shoot me

                            K 1 Reply Last reply
                            0
                            • L locoone

                              unsigned long id; or DWORD id; either of them will give another error if i try them and i cant even come close of making since of the web page :mad: just shoot me

                              K Offline
                              K Offline
                              khan
                              wrote on last edited by
                              #14

                              First thing to do is Be Calm. Read some poetry. Next, declaring a variable does not give errors. Make sure you declare it: DWORD id; before CreateThread() function call. And try Rebuild All from the build menu. If that does not solve the problem, you can post the error that Visual Studio gives. this is this.

                              L 1 Reply Last reply
                              0
                              • K khan

                                First thing to do is Be Calm. Read some poetry. Next, declaring a variable does not give errors. Make sure you declare it: DWORD id; before CreateThread() function call. And try Rebuild All from the build menu. If that does not solve the problem, you can post the error that Visual Studio gives. this is this.

                                L Offline
                                L Offline
                                locoone
                                wrote on last edited by
                                #15

                                C:\Program Files\Microsoft Visual Studio\MyProjects\wordlist\wordlistDlg.cpp(231) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type DWORD id; m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); that was with the build all ps. i am calm lol

                                K 1 Reply Last reply
                                0
                                • L locoone

                                  C:\Program Files\Microsoft Visual Studio\MyProjects\wordlist\wordlistDlg.cpp(231) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type DWORD id; m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); that was with the build all ps. i am calm lol

                                  K Offline
                                  K Offline
                                  khan
                                  wrote on last edited by
                                  #16

                                  Did you declare the Thread function in the class? It should be like this: static DWORD __stdcall Thread(LPVOID lpVoid); In the implementation file: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { ... And "CMyDlg" is the name of your dialog class. Change that with the real name. Hope that helps. this is this.

                                  L 1 Reply Last reply
                                  0
                                  • K khan

                                    Did you declare the Thread function in the class? It should be like this: static DWORD __stdcall Thread(LPVOID lpVoid); In the implementation file: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { ... And "CMyDlg" is the name of your dialog class. Change that with the real name. Hope that helps. this is this.

                                    L Offline
                                    L Offline
                                    locoone
                                    wrote on last edited by
                                    #17

                                    im so lost now i have no clue

                                    K 1 Reply Last reply
                                    0
                                    • L locoone

                                      im so lost now i have no clue

                                      K Offline
                                      K Offline
                                      khan
                                      wrote on last edited by
                                      #18

                                      Note that CMyDlg is the placeholder of your class-name. e.g, if you made a dialog-based app called Hello, then your dialog class-name will be "CHelloDlg". When you declare a function, it means go to the header file (*.h), and write the declaration line there: e.g, in HelloDlg.h: HANDLE m_hThread; bool m_bWorking; static DWORD __stdcall Thread(LPVOID lpVoid); //------------- declaration ends here. Now open the associated .cpp file e.g, HelloDlg.cpp and write the complete functions there: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { ..... blah blah. And be sure to read a good book on Visual C++. this is this.

                                      L 1 Reply Last reply
                                      0
                                      • K khan

                                        Note that CMyDlg is the placeholder of your class-name. e.g, if you made a dialog-based app called Hello, then your dialog class-name will be "CHelloDlg". When you declare a function, it means go to the header file (*.h), and write the declaration line there: e.g, in HelloDlg.h: HANDLE m_hThread; bool m_bWorking; static DWORD __stdcall Thread(LPVOID lpVoid); //------------- declaration ends here. Now open the associated .cpp file e.g, HelloDlg.cpp and write the complete functions there: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { ..... blah blah. And be sure to read a good book on Visual C++. this is this.

                                        L Offline
                                        L Offline
                                        locoone
                                        wrote on last edited by
                                        #19

                                        yea i know about the CMyDlg name i got it to not give a error when compilling but when run is a bad error but its 6 am im going to bed will post tomorrow

                                        1 Reply Last reply
                                        0
                                        • K khan

                                          I assume this is your worker function: void CMyDlg::Worker() { while (m_bWorking) { //processing here. } } In your header file: HANDLE m_hThread; bool m_bWorking; static DWORD __stdcall Thread(LPVOID lpVoid); In your .cpp file: DWORD __stdcall CMyDlg::Thread(LPVOID lpVoid) { CMyDlg* parent = (CMyDlg*)lpVoid; parent->Worker(); return 0; } void CMyDlg::OnButtonStart()//your button handler function. { m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); } void CMyDlg::OnButtonSuspend()//pause it { SuspendThread(m_hThread); } void CMyDlg::OnButtonResume()//resume it { ResumeThread(m_hThread); } void CMyDlg::OnButtonStop()//stop it { m_bWorking = false; } I hope that helps. this is this.

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #20

                                          khan++ wrote: m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); Use AfxBeginThread() with MFC applications.


                                          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups