Getting control back from while(1)
Hello,
I am having a simple dialog based application.
On selection of a radio button the application continously reads a IrDA port for data.
But I am unable to come of out of this while(1) loop. I have a stop button. Clicking on it needs to close the IrDA port and wait for other operations.
But control is not getting back. I had put AfxMessageBox and it is not coming. So what is the way out
CDialogTest::OnCheck()
{
while(1)
ReadIrDA();
Sleep(100);
}
Please help me this is very urgent
thanks & regards
Raghav
# 1 Re: Getting control back from while(1)
There is no condition to stop the while.
I should think about something else then a while, for example a worker thread which sends a message when something is connected. If you want to stop, you send a WM_QUIT message to the thread.
If you want to use the while anyway, it should look something like this:
bool bStop = false;
while (!bStop)
{
// Read hardware
if (ReadIrDA())
bStop = true;
}But I should go for the first option...
# 2 Re: Getting control back from while(1)
Your program cannot answer to any command or message when your program is in a loop, because the WindowProc is not called until you exit that loop.
The solution for your program would be a timer.
See MSDN for SetTimer (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cwnd.3a3a.settimer.asp)
szz at 2007-11-11 0:28:15 >

# 3 Re: Getting control back from while(1)
Hello All,
I think my question was not properly framed.
I want IrDA read operation to be continous. That is there will be two button one for start and other for stopping it. When Start button is pressed it should continously poll for IrDA port and when stop button is pressed it should stop this operation. Using while(1) is not mandatory.
Please help me out. It is urgent
thanks & regards
Raghav
# 4 Re: Getting control back from while(1)
I faced the same problem in my own application, reading the serial port.
The only solution I know:
1. Put your continous loop in a worker thread.
2. Change while(1) to while(ShallRun)
3. Your application can now react on messages, as the continous loop is in the worker. So the only thing to do is to change the variable ShallRun from TRUE to FALSE.
4. When your application closes use WaitForSingleObject to find out that your thread is finished.
Marc
# 5 Re: Getting control back from while(1)
First, your loop is infinite and will be... because your application does NOT have any chance to handle your click on the Stop button.
Next, you wrote there Sleep(100);... I think you wanted that inside the loop => you forgot the "{}".
And you should really look at the SetTimer, because i think it's the easiest solution, however Tischnoetentoet posted another good solution to your problem.
You should have something like this:
void CYourDlg::OnStart()
{
m_nTimer = SetTimer(ID_READ_NOW, 100, 0);
}
void CYourDlg::OnStop()
{
KillTimer(m_nTimer);
}
void CYourDlg::OnTimer(UINT nIDEvent)
{
if (nIDEvent == ID_READ_NOW)
ReadIrDA();
// Call base class handler.
CDialog::OnTimer(nIDEvent);
}
szz at 2007-11-11 0:31:23 >

# 6 Re: Getting control back from while(1)
yes, timer is a good solution too, depending on the time the code takes to read from the port. If it takes a lot of time, it can happen that it seems your UI "hangs" because your application is reading the port.
So if the reading is very fast, use a timer (easy), if not, use a worker thread (UI will always be available). It's your choice!
# 7 Re: Getting control back from while(1)
Hello All,
Thanks . it is working fine. I have used timer based.
thanks & regards
Raghav
# 8 Re: Getting control back from while(1)
Usually, when you must performs such operations like reading or writing to/from a port, you want to put the code that does that in separate (working) thread, and remove that responsibility from the UI thread, so you can process user's commands.
cilu at 2007-11-11 0:34:24 >
