Urgent: Having problem with while loop
-
Hi, I am working with the MFC which is connected to the database using recordset. However, I currently encounter some problem. Let me explain further what this program is abt. For this program that i am working on now, i have an update button. Also, there are 3 parameters which i want to keep changing the values on its own once the update button is being clicked. This might need to use "while loop" and i would like to change the values between the range of 1 to 5 only. This looping will only stop when i click the close on the window itself. However, the problem now is that i need to change the values when i input it myself and press the update button before it can be stored inside the database. So i am hoping that anyone who knows how to get this thing workin to give me some ideas on how to go abt doin this. Thanks
-
Hi, I am working with the MFC which is connected to the database using recordset. However, I currently encounter some problem. Let me explain further what this program is abt. For this program that i am working on now, i have an update button. Also, there are 3 parameters which i want to keep changing the values on its own once the update button is being clicked. This might need to use "while loop" and i would like to change the values between the range of 1 to 5 only. This looping will only stop when i click the close on the window itself. However, the problem now is that i need to change the values when i input it myself and press the update button before it can be stored inside the database. So i am hoping that anyone who knows how to get this thing workin to give me some ideas on how to go abt doin this. Thanks
The solution is very simple: THREADS (it's easy too ;)) I give the sample code in Windows API. I m sure u can port it to MFC. I have given MFC portions that i can predict(lol, i dont know much MFC). //First of all this is the thread which will continiously update ur value when update button is clicked.
//------global data------ HANDLE hthread; // this is thread handle int idThread; // this is threaed id //---The thread function------------------------ DWORD WINAPI UpdateThread(LPVOID lpParameter) { while(1) //keep looping { //WRITE YOUR UPDATING CODE HERE THAT WILL CONTINIOUSLY UPDATE THE VALUES //Make the values to be updated GLOBAL as there is no way to return values from a thread. } }
// This function is called when Update Button is clicked(name it according to ur program)OnUpdateButtonClick() { //when update button is clicked, start the update thread should be started hThread=CreateThread(NULL,0,UpdateThread,0,0,&idThread); //dont bother about the parameters that are NULL or 0, u ll never need them }
Now thread executes repeatedly irrespective of ur main program code. We want to stop the thread when STOP button is clicked. So we write the following code:OnStopButtonClick() { //We sud stop the thread now (i.e. stop the updating loop inside the thread) TerminateThread(hThread,0) //WRITE HERE "UR CODE TO WRITE UPDATED VALUES INTO THE DATABASE"} }
I think, this will solve ur problem, ask if u have any doubts.
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
The solution is very simple: THREADS (it's easy too ;)) I give the sample code in Windows API. I m sure u can port it to MFC. I have given MFC portions that i can predict(lol, i dont know much MFC). //First of all this is the thread which will continiously update ur value when update button is clicked.
//------global data------ HANDLE hthread; // this is thread handle int idThread; // this is threaed id //---The thread function------------------------ DWORD WINAPI UpdateThread(LPVOID lpParameter) { while(1) //keep looping { //WRITE YOUR UPDATING CODE HERE THAT WILL CONTINIOUSLY UPDATE THE VALUES //Make the values to be updated GLOBAL as there is no way to return values from a thread. } }
// This function is called when Update Button is clicked(name it according to ur program)OnUpdateButtonClick() { //when update button is clicked, start the update thread should be started hThread=CreateThread(NULL,0,UpdateThread,0,0,&idThread); //dont bother about the parameters that are NULL or 0, u ll never need them }
Now thread executes repeatedly irrespective of ur main program code. We want to stop the thread when STOP button is clicked. So we write the following code:OnStopButtonClick() { //We sud stop the thread now (i.e. stop the updating loop inside the thread) TerminateThread(hThread,0) //WRITE HERE "UR CODE TO WRITE UPDATED VALUES INTO THE DATABASE"} }
I think, this will solve ur problem, ask if u have any doubts.
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
Hi hi, Thanks for the codings that is provided. Much appreciated. However, i am unfamiliar with using THREADS. But will give it a try on the codings that you provided. In the meantime, is there any other solution to this problem?? Thanks lots:)
Hey, i've got another solution for u (that too WITHOUT threads ;)) //----------make a global var---------- //glo can take two values>> //0:do nohting; 1:update the values;
int glo=0;
DO idle-time processing- i think the function is OnIdle() forCwinApp
(i'm not sure about MFC, find it urself) TheOnIdle
function is executed when ur window doesnt receive any key input.OnIdle
is executed all time but it will update ur values only when glo is 1. The following code will ensure this:CWinApp::OnIdle() { if(glo==0) ; //do nothing else if(glo==1) { //**write all ur updating stuff here** } }
Inside Update Button click function, all u have to do is to set the value of glo to 1. //---------Update button click rotine-----OnUpdateClick() { glo=1; }
Inside Stop Button click function, all u have to do is to reset the value of glo to 0. This will stop the update.(;)) //---------Update button click rotine-----OnStopClick() { glo=0; //NOW WRITE UPDATED VALUES TO DATABASE }
Thats allllllll........... ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
The solution is very simple: THREADS (it's easy too ;)) I give the sample code in Windows API. I m sure u can port it to MFC. I have given MFC portions that i can predict(lol, i dont know much MFC). //First of all this is the thread which will continiously update ur value when update button is clicked.
//------global data------ HANDLE hthread; // this is thread handle int idThread; // this is threaed id //---The thread function------------------------ DWORD WINAPI UpdateThread(LPVOID lpParameter) { while(1) //keep looping { //WRITE YOUR UPDATING CODE HERE THAT WILL CONTINIOUSLY UPDATE THE VALUES //Make the values to be updated GLOBAL as there is no way to return values from a thread. } }
// This function is called when Update Button is clicked(name it according to ur program)OnUpdateButtonClick() { //when update button is clicked, start the update thread should be started hThread=CreateThread(NULL,0,UpdateThread,0,0,&idThread); //dont bother about the parameters that are NULL or 0, u ll never need them }
Now thread executes repeatedly irrespective of ur main program code. We want to stop the thread when STOP button is clicked. So we write the following code:OnStopButtonClick() { //We sud stop the thread now (i.e. stop the updating loop inside the thread) TerminateThread(hThread,0) //WRITE HERE "UR CODE TO WRITE UPDATED VALUES INTO THE DATABASE"} }
I think, this will solve ur problem, ask if u have any doubts.
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
Hi, For this thread function, i have some doubts which i have stated below. Can kindly explain to me? Thanks For this function that you have stated:
DWORD WINAPI UpdateThread(LPVOID lpParameter)
I just need to copy down or do i have to state any values to the lpParameter? Here, i have also attached part of the codings that i have done, but i have tried the methods that you have given but it dun seems to work the way as you state. The coding stated below, the while loop is working and the database will be updated on click the UpdateDB. however, once i click on the update button, the whole program seems to hang.//global variables int check_exit=0; //Update button function void CUpdateView::OnUpdatedb() { // TODO: Add your control notification handler code here //get a pointer to the recordset CUpdateDoc* pDoc = GetDocument(); UseRecordset* pUseRecordset = &pDoc->m_UseRecordset; int x = 5; do { pUseRecordset->Edit(); //UpdateData(true); pUseRecordset->m_propatime = m_propagation*x; pUseRecordset->Update(); if (check_exit = 0) check_exit=1; }while(check_exit!=1); //::MessageBox(0, "Updated!", "Information",0); } //Exit button function void CUpdateView::OnExit() { check_exit = 0; }
Thanks a million, dreamerzz -
Hi, For this thread function, i have some doubts which i have stated below. Can kindly explain to me? Thanks For this function that you have stated:
DWORD WINAPI UpdateThread(LPVOID lpParameter)
I just need to copy down or do i have to state any values to the lpParameter? Here, i have also attached part of the codings that i have done, but i have tried the methods that you have given but it dun seems to work the way as you state. The coding stated below, the while loop is working and the database will be updated on click the UpdateDB. however, once i click on the update button, the whole program seems to hang.//global variables int check_exit=0; //Update button function void CUpdateView::OnUpdatedb() { // TODO: Add your control notification handler code here //get a pointer to the recordset CUpdateDoc* pDoc = GetDocument(); UseRecordset* pUseRecordset = &pDoc->m_UseRecordset; int x = 5; do { pUseRecordset->Edit(); //UpdateData(true); pUseRecordset->m_propatime = m_propagation*x; pUseRecordset->Update(); if (check_exit = 0) check_exit=1; }while(check_exit!=1); //::MessageBox(0, "Updated!", "Information",0); } //Exit button function void CUpdateView::OnExit() { check_exit = 0; }
Thanks a million, dreamerzzif (check_exit = 0)
If this is what you have coded, then change it to:if (check_exit == 0)
LOOK CLOSE AT UR PROGRAM, it doesnt come out of the do-while loop Well, if you have written that by mistake and are actually usingif (check_exit == 0)
then your program is hanging because it keeps looping inside thedo-while
LOOP. ACTUALLY UR PROGRAM APPEARS TO HANG(loops & loops and loops) AS IT DOESNT RETURN CONTROL BACK TO WINDOWS(ur main program), so you can no longer interact with any other buttons. The trick is to run a continious LOOPING do-while LOOP, but we want it to run seperately from our main program. HERE COMES THE THREAD:::>>>int loop=1; //1 means keep looping 0 means stop and end the thread
DWORD WINAPI UpdateThread(LPVOID lpParameter)
{
//get a pointer to the recordset
CUpdateDoc* pDoc = GetDocument();
UseRecordset* pUseRecordset = &pDoc->m_UseRecordset;
int x = 5;
**while(loop==1)
{
pUseRecordset->Edit();//UpdateData(true);
pUseRecordset->m_propatime = m_propagation*x;pUseRecordset->Update();
}**
}THE ABOVE FUNCTION DOES NOTHING. IT DOESNOT START A NEW THREAD. It is like any ordinary function function defination. Its has to be called from somewhere to make it execute. BUT UNLIKE ORDINARY FUNTIONS, we will not call UpdateThread(<>) as it will not start a new thread. And the name "UpdateThread" is purely arbitrary. You can name the thread as "dreamerzz Thread()" or "CrapThread()" or "BoomBoom()" or whatever you want. Now, we want this thread to start when we click Update buttom. so, we write::>>>
//------global data------
HANDLE hthread; // this is thread handle
int idThread; // this is threaed id//Update button function
void CUpdateView::OnUpdatedb()
{
// TODO: Add your control notification handler code here// in place of "UpdateThread" use whatever you have named the thread
hThread=CreateThread(NULL,0,UpdateThread,0,0,&idThread);
}You dont have to explicitly end the thread as it will automatically terminate when the value of
"loop"
becomes 0. (To explicitly terminate a thread you can use the functionTerminateThread(hThread,0);
)//Exit button function
void CUpdateView::OnExit()
{
loop = 0;
}Ask if u have doubts ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
if (check_exit = 0)
If this is what you have coded, then change it to:if (check_exit == 0)
LOOK CLOSE AT UR PROGRAM, it doesnt come out of the do-while loop Well, if you have written that by mistake and are actually usingif (check_exit == 0)
then your program is hanging because it keeps looping inside thedo-while
LOOP. ACTUALLY UR PROGRAM APPEARS TO HANG(loops & loops and loops) AS IT DOESNT RETURN CONTROL BACK TO WINDOWS(ur main program), so you can no longer interact with any other buttons. The trick is to run a continious LOOPING do-while LOOP, but we want it to run seperately from our main program. HERE COMES THE THREAD:::>>>int loop=1; //1 means keep looping 0 means stop and end the thread
DWORD WINAPI UpdateThread(LPVOID lpParameter)
{
//get a pointer to the recordset
CUpdateDoc* pDoc = GetDocument();
UseRecordset* pUseRecordset = &pDoc->m_UseRecordset;
int x = 5;
**while(loop==1)
{
pUseRecordset->Edit();//UpdateData(true);
pUseRecordset->m_propatime = m_propagation*x;pUseRecordset->Update();
}**
}THE ABOVE FUNCTION DOES NOTHING. IT DOESNOT START A NEW THREAD. It is like any ordinary function function defination. Its has to be called from somewhere to make it execute. BUT UNLIKE ORDINARY FUNTIONS, we will not call UpdateThread(<>) as it will not start a new thread. And the name "UpdateThread" is purely arbitrary. You can name the thread as "dreamerzz Thread()" or "CrapThread()" or "BoomBoom()" or whatever you want. Now, we want this thread to start when we click Update buttom. so, we write::>>>
//------global data------
HANDLE hthread; // this is thread handle
int idThread; // this is threaed id//Update button function
void CUpdateView::OnUpdatedb()
{
// TODO: Add your control notification handler code here// in place of "UpdateThread" use whatever you have named the thread
hThread=CreateThread(NULL,0,UpdateThread,0,0,&idThread);
}You dont have to explicitly end the thread as it will automatically terminate when the value of
"loop"
becomes 0. (To explicitly terminate a thread you can use the functionTerminateThread(hThread,0);
)//Exit button function
void CUpdateView::OnExit()
{
loop = 0;
}Ask if u have doubts ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs