loop speed
-
maybe use the Sleep(1); function. That will make the thread sleep for 1ms so that other threads can execute.
-
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.
-
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.
-
#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.
-
#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.
-
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.
-
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.
-
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.
-
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
-
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.
-
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
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.
-
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.
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
-
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
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.
-
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.
-
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.
-
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.
-
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.
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
-
i narrowed it down i think. if i leave DWORD id; m_bWorking = TRUE; m_hThread = CreateThread(NULL,0,Thread,this,CREATE_SUSPENDED,&id); SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); ResumeThread(m_hThread); like it is it errors if i //ResumeThread(m_hThread); no error but the program dont run either so im stuck again. error: debug assertion failed file: wincore.cpp line: 890 X| X| X| X|