SImple question about While loops
How can I get a while loop to run... and end when a button (Such as stop in this case) is pressed.
such as (NOTE: I KNOW THIS IS WRONG)
while (OnStop() is not pushed)
{
//function
}
handler = OnStop()
object = IDC_Stop
# 4 Re: SImple question about While loops
this is not the whole project, but this is what I did before
you can start with a Stop and Start button, when the start button is click data is being process but other event are still possible, so I have a boolean variable to check the loop, this variable is set to true when stop is click;
bool bStop
OnStop()
{
bStop = true;
}
OnStart()
{
while( !bStop )
{
CheckEventStop(); --> event checker function
}
}
CheckEventStop()
{
MSG msg;
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ideru at 2007-11-10 23:57:24 >

# 5 Re: SImple question about While loops
I see, you step forward in your problem... :-)
The following shall only show you some hints. It looks like C-Code, but it is not.
You got two Buttons in your applications: StartBtn and StopBtn
BOOL StopMe;
OnStartBtn()
{
StopMe = FALSE;
AfxBeginThread(MyThreadFunct, PRIORITY_NORMAL);
}
On StopBtn()
{
StopMe = TRUE;
}
OnMYMessage()
{
//SetDlgItemText(...);
UpdateData(FALSE);
}
MyThreadFunct()
{
While(!StopMe)
CalculateWhatEverYouWant();
SendMessage(MYMESSAGE, 0,0)
}
In result, your Allication will be split up into a User interface that does almost nothing, and a Worker thread that calculates with no pause.
Your next problem will be, that you want to see some data in the meantime. As your worker can't access the GUI, it has to tell the GUI about the change. You might use global variables for information-transport within your application.
But in your other thread your problem seemed to be solved better (easier) in a timer-function...
Marc
# 6 Re: SImple question about While loops
Hi ILovYou3232!
I answer your private message in the open board, as I think the answer might either be not the best available and/or it might be interesting for other people.
In the following I give you an example out of my own program. The creation of a new thread is easier than expected. Just create a function that follows the rules and call it with the described command. Finished.
Most of the following code describes how to get information out of the thread. This is a little bit more complicated, as you need to build something like a telephone line: The sender needs to know hwo to call, the receiver needs to have a valid phone number, the sender must not call if the receiver not defined at all, and the receiver needs to know what to do if get called. Beside that you have to think about how often you want to call, how to react when the receiver is not picking up the phone immedeately, and many other tasks. All the message-related code below works fine for my application, but this is not neccessarily the right choose for you.
You might skip all the messaging stuff first and you will find that almost nothing remains...
/*For Hand-Made Messages you need to tell both the sender and the receiver what to talk about. Here is the sender:*/
const UINT WM_COMMSTATE = RegisterWindowMessage("MYMSG:WM_COMMSTATE");
/*
Start the thread. This means just "create a second CPU that does nothing else than to process this function." If this function ends, the CPU is destroyed again. Most important: This call here just creates the CPU, and immedeately continues here. Your application is not stopped here.
You might place the following line in your OnInitialUpdate() of your view:*/
AfxBeginThread(ServerProc, GetSafeHwnd(), THREAD_PRIORITY_NORMAL);
/*In My case I needed a "server", so my function is called "serverproc". Windows requires that it takes a parameter of type LPVOID. I personally do not need this one, but if windows requires it, it shall get what it wants...
*/
UINT ServerProc(LPVOID pParam)
{
//This is just a function, like any other function. it is not inside a class or whatever.
//Make your calculation here
/*This way I send a message to the GUI. I have a variable of type HWND, called HwndServiceView. Only if it is valid, I send the message to the GUI.
I guess it's a bug to store the HWND-Value in a variable for later use, but I didn't know a better solution that time (and still today).
I defined the Message handler by myself, In this case via a define called WM_COMMSTATE. The CC_GET_VAL_DECIMALS ist just a number. This way I transport information to the GUI. You might want to place a "0" there.
*/
if (HwndServiceView) PostMessage(HwndServiceView, WM_COMMSTATE,0, CC_GET_VAL_DECIMALS);
return 0;
}
In my applications main view I have to add this line, Let's say it defines the receiver:
static const UINT WM_COMMSTATE = RegisterWindowMessage("MYMSG:WM_COMMSTATE");
/* in the same file, just inside the section "MessageMap", outside the automated generated secion, I have to declare, which function to call:*/
ON_REGISTERED_MESSAGE(WM_COMMSTATE, OnCommState)
I need to define a message handler for the User defined message. I do it in the Header file, "protected" area.
afx_msg LRESULT OnCommState(WPARAM wParam, LPARAM lParam);
//Each hand-made Message-handler must have those two parameters...
The called function itself is still missing. Here it comes:
LRESULT CServiceView::OnCommState(WPARAM wParam, LPARAM lParam)
{
/*Do what you want here, i. e. SetDlgItemText*/
return 0L /*Windows requires it..*/
}
Good luck!
Marc