Solved

Closing while in DoEvents causes exception

Posted on 1998-10-29
13
430 Views
Last Modified: 2013-12-26
I'm a VC programmer that used VB to create a control to embed in my MFC MDI Application.  I am getting an unhandled exception when I close my MFC application while my VB control is in a paint loop.  I have a DoEvents in the loop, which, if I remove, prevents the exception from occuring but then I can't process other messages.  
My question is:  Is there some way to do the equivalent of an ::AfxLockApp() in VB?  I think the rug is getting pulled out from underneath my control while it's in this paint loop.  I just want to lock things down while it's in this paint loop so it can't go away until it finishes.
Thanks in advance
0
Comment
Question by:skin
  • 7
  • 5
13 Comments
 
LVL 15

Expert Comment

by:ameba
Comment Utility
paint loop with DoEvents? Can we see this portion of code, and where did you put it.
And what does AfxLockApp do?
0
 

Author Comment

by:skin
Comment Utility
I should have said AfxOleLockApp().  It just keeps the OLE object locked down so it can't go away until the AfxOleUnlockApp() is called    
Here's the code. Basically it is doing some calculations and then calling the Picture method on an ImageArray:

For i = 0 To m_nPagesInOpenDoc - 1
    DoEvents
        If imgArray(i).Visible = True Then
            ' If it doesn't have a picture, create the picture
                If imgArray(i).Picture = 0 Then
                    IDMViewerCtrl1.pageNumber = i + 1
                    Set pg = IDMViewerCtrl1.Page
                    Dim pixX As Integer
                    Dim pixY As Integer
                    pixX = m_propThumbWidth / Screen.TwipsPerPixelX
                    pixY = m_propThumbHeight / Screen.TwipsPerPixelY
                    Set imgArray(i).Picture = pg.Picture(pixX, pixY, idmBrightnessEnhance, 0)
                    ' The picture can be a little larger than we asked for.  Fix up the text
                    lblNumArray(i).Top = imgArray(i).Top + imgArray(i).Height + m_propThumbBorder / 4
                    shpSelection(i).Top = imgArray(i).Top - selborder * Screen.TwipsPerPixelY
                    shpSelection(i).Left = imgArray(i).Left - selborder * Screen.TwipsPerPixelX
                    shpSelection(i).Width = imgArray(i).Width + selborder * 2 * Screen.TwipsPerPixelX
                    shpSelection(i).Height = imgArray(i).Height + selborder * 2 * Screen.TwipsPerPixelY
                End If
        End If  ' visible
    Next i

0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
I think your goal should be avoiding DoEvents. This is why I asked for this code.

If you cannot avoid DoEvents:
' declarations of your user control
Private mExecuting As Integer

At the top of your loop add:
    mExecuting = 1
In your paint loop add this after each 2-3 paint operations:
        If mExecuting = 0 Then
            ' some exit operations, e.g mousepointer=vbNormal
            Exit Sub
        End If
' in other words, often chack mExecuting flag

Here you will set flag to 0, allow processing of small portion (2-3 lines) of your paint loop, and terminate all
Private Sub UserControl_Terminate()
    mExecuting = 0
    Doevents ' allow 2-3 lines
    ' now set all object references to Nothing
.
    End  ' brute force, but sometimes we cannot do any better
         ' you can also try without it
End Sub

Other causes:
Do you have circular references in your VB code?

But, first, let's try to avoid DoEvents
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
Sorry, my comment was added before I saw your code. I will read it in a moment.
0
 

Author Comment

by:skin
Comment Utility
I really can't get away from using DoEvents and I do not suspect any circular references.  My best guess is that the rug is being pulled out from underneath my control.  So far I have been debugging in the VC Dev studio and I only see VB on the call stack when the error occurs.  
0
 

Author Comment

by:skin
Comment Utility
I really can't get away from using DoEvents and I do not suspect any circular references.  My best guess is that the rug is being pulled out from underneath my control.  So far I have been debugging in the VC Dev studio and I only see VB on the call stack when the error occurs.  
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:skin
Comment Utility
I really can't get away from using DoEvents and I do not suspect any circular references.  My best guess is that the rug is being pulled out from underneath my control.  So far I have been debugging in the VC Dev studio and I only see VB on the call stack when the error occurs.  
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
OK. Can you try the second (mExecuting flag)
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
In the mean time 2 small tips (when you'll have time):
Use With keyword to reduce number of dots:
    For i = 0 To m_nPagesInOpenDoc - 1
        DoEvents
        With imgArray(i)
            If .Visible = True Then
            ....etc
        End With

Intead of setting shpSelection(i).Top, left , Width
use Move method:
Dim tmpTop as single
Dim tmpLeft as single
tmpTop = ...
tmpLeft = ...
shpSelection(i).Move tmpTop, tmpLeft, tmpWidth, tmpHeight

0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
At the top of your loop add:
    mExecuting = 1 ' before loop
but I forgot reseting flag:
    mExecuting = 0 ' after your loop

' checking flag:
       If mExecuting = 0 Then Exit Sub


In UserControl_Terminate add more doevents (1 to 100 ?)
    Doevents ' allow 2-3 lines
    Doevents
    Doevents

0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 200 total points
Comment Utility
DOEVENTS == CRASH!!!

This is just a fact. Show me in an app that does doevents where it does doevents and I can make it crash.

DoEvents causes VB to stop what it is doing and go into the messageloop again. DoEvents basically equals TranslateMessage and DispatchMessage. Would you as a C programmer ever use that in a C application? I don't think so.
DoEvents causes looping of events and reentrency of code.

DoEvents can always be removed by starting a timer with a delay of one. In your case

private sub form_paint
     if (not m_bInPaint) then  'prevent reentrancy
           m_bInPaint = true
           m_iPainting = 1
           timerpaint.enabled = true
     end if
end sub

private sub timerpaint_timer
   if (m_iPainting <= m_nPagesInOpenDoc) then
        PaintOpenDoc  m_iPainting
        m_iPainting = m_iPainting + 1
   else
        timerpaint.enabled = false
        m_bInPaint =false
   end if
end sub

0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
I don't think skin put his code in "Sub UserControl_Paint()".
If he did, I agree with Mirkwood - this is disaster. Paint event will be raised again during Doevents.
But I don't think he is entering his paint loop again while in a loop.
My example where I was forced to use End is in Form, not in UserControl. It is controlled loop, which checks for existence of a file. There is no reentering the loop. Doevents is in loop to allow user pressing "Exit" or "Quit" button.
When button is pressed, I have to unload form and use End or ExitProcess.
I described how I solved problem, but this is for "in house use" util, and I didn't look for better solution because it worked.

I will see if I can use Mirkwoods' "Timer" solution.
0
 

Author Comment

by:skin
Comment Utility
Thanks for the suggestions.  As it turns out when I used the member variable idea ameba suggested I found out it is not throwing the exception when in the DoEvents.  I have not yet figured out how to tell where the exception is coming from.  Since these are good suggestions, I'll close this and keep trying to figure out where the exception is coming from.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now