DizZzM
asked on
UpdateAllViews Crashes My App
Hi Experts -
Easy question (hopefully) - developing a serial data monotoring utility. Every time I get new data on the bus I want to display it in the main window. The set-up I have now is - the user hits a run button which calls onRun() from myDoc class - it opens up the serial port and it goes into an infinite while loop that polls the bus. If there is data on the bus - then it reads it and it calls UpdateAllViews(NULL) to make a call to pDC->TextOut(). This is crashing my application - any ideas?
Also - is it possible to put a flag on that infinite loop that'll get reset by another button (call it Stop)? So for example...
void MyDoc::OnCommandStop()
{
stop_recv_flag = 1;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
So if I hit the stop button to call OnStop will it break the while loop if stop_recv_flag is a global variable?
Easy question (hopefully) - developing a serial data monotoring utility. Every time I get new data on the bus I want to display it in the main window. The set-up I have now is - the user hits a run button which calls onRun() from myDoc class - it opens up the serial port and it goes into an infinite while loop that polls the bus. If there is data on the bus - then it reads it and it calls UpdateAllViews(NULL) to make a call to pDC->TextOut(). This is crashing my application - any ideas?
Also - is it possible to put a flag on that infinite loop that'll get reset by another button (call it Stop)? So for example...
void MyDoc::OnCommandStop()
{
stop_recv_flag = 1;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
So if I hit the stop button to call OnStop will it break the while loop if stop_recv_flag is a global variable?
>>then I get an application error
What kind of error you are getting ?
-MAHESH
What kind of error you are getting ?
-MAHESH
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi mahesh - it's a sharing violation. I step through with the debugger and OnStop() does execute and stop_recv_flag does get modified. The error occurs on return. Do I need some mutex around stop_recv_flag?
The weird part is I kept developing and troubleshooting last night and I started noticing that the error does not occur every single time which makes me further believe I just need some mutex around the variable but maybe I'm wrong.
Hi Andy - thanks for your solution. Got that part working after I posted my original question with AfxGetApp()->PumpMessage() ;.
The weird part is I kept developing and troubleshooting last night and I started noticing that the error does not occur every single time which makes me further believe I just need some mutex around the variable but maybe I'm wrong.
Hi Andy - thanks for your solution. Got that part working after I posted my original question with AfxGetApp()->PumpMessage()
My understanding is that the PumpMessage will only force the next message in the queue to be handled. A DoEvents type function will force all waiting messages to be handled.
>>The error occurs on return.
return means exactly where ? In loop ?? have you traced that ??? you may put a simple MessageBox() before UpdateAllViews to trace that...
-MAHESH
return means exactly where ? In loop ?? have you traced that ??? you may put a simple MessageBox() before UpdateAllViews to trace that...
-MAHESH
ASKER
return from the OnStop() call. I'll do some more analysis now and see if I can dig any deeper.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Guys - just ended up using a CMutex around the stop_recv_flag assignment and it worked! I'll just split the points between you guys. Thanks again!
CMutex recv_Mutex;
void MyDoc::OnCommandStop()
{
recv_Mutex
stop_recv_flag = 1;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
CMutex recv_Mutex;
void MyDoc::OnCommandStop()
{
recv_Mutex
stop_recv_flag = 1;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
ASKER
Sorry - got trigger happy on the send... heres the solution:
CMutex recv_Mutex;
void MyDoc::OnCommandStop()
{
recv_Mutex.Lock;
stop_recv_flag = 1;
recv_Mutex.Unlock;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
AfxGetApp()->PumpMessage() ;
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
CMutex recv_Mutex;
void MyDoc::OnCommandStop()
{
recv_Mutex.Lock;
stop_recv_flag = 1;
recv_Mutex.Unlock;
}
void MyDoc::OnCommandRun()
{
while(!stop_recv_flag)
{
status = read_bus (data);
AfxGetApp()->PumpMessage()
if (status == ARS_GOTDATA)
{
UpdateAllViews(NULL);
}
}
stop_recv_flag = 0;
}
ASKER
Found the solution, just needed to add
AfxGetApp()->PumpMessage()
inside of the while loop.
The 2nd question is still an open issue however so the points are still up for grabs. For some reason, when I hit OnStop(), the global 'stop_recv_flag' does get set but then I get an application error and it shuts down. I would have thought OnRun would have read the correct new value and kicked out of the loop nicely.
Any ideas?