Solved

UpdateAllViews Crashes My App

Posted on 2006-10-26
10
272 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:DizZzM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:DizZzM
ID: 17816109
Hi Experts -

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?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17817419
>>then I get an application error

What kind of error you are getting ?

-MAHESH
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 17817552
I guess that
void MyDoc::OnCommandStop()
{
     stop_recv_flag = 1;
}
is never being called when you hit the stop button.  Try putting a breakpoint onto the  stop_recv_flag = 1; and seeing if it is reached.



also
void DoEvents()
{  
      MSG msg;  
      while ( ::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ) )
      {  
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
      }
}

void MyDoc::OnCommandRun()
{
     while(!stop_recv_flag)
     {
          status = read_bus (data);
          if (status == ARS_GOTDATA)
          {
               UpdateAllViews(NULL);    
          }  
DoEvents();   <<---------------- allow the message queue to be processed                
     }

     stop_recv_flag = 0;
     
     
}

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DizZzM
ID: 17819152
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();.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17819214
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.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17819755
>>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
0
 

Author Comment

by:DizZzM
ID: 17819858
return from the OnStop() call.  I'll do some more analysis now and see if I can dig any deeper.
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 250 total points
ID: 17820010
You have overrideen function OnUpdate() for all / any of your views ?

As said you may put messagebox before/after UpdateAllViews() to ensure its happening only when UpdateAllViews executed..

-MAHESH
0
 

Author Comment

by:DizZzM
ID: 17820360
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;
     
     
}
0
 

Author Comment

by:DizZzM
ID: 17820386
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;
     
     
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 139
Separate into files by filename 12 91
java ^ examples 8 70
Making an alias 7 114
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question