Loops not looping without a messagebox

Loops not looping without a messagebox

loicLPQZG
Contributor Contributor
127 Views
2 Replies
Message 1 of 3

Loops not looping without a messagebox

loicLPQZG
Contributor
Contributor

Hello all,

 

I have a bit of a silly question as i don't have a strong background in programming.

 

I try to loop through center marks to add breaks withing a drawing.

I call a different sub with 2 centermarks as references.

But my loop stops after placing 1 break (when i do not display a messagebox).

But when I do display a messagebox for each time it loops, it loops just fine to the end.

 

I have tried for and while loops but get the same result.

How Could i resolve this so i don't get 50 message boxes when i try to get to the end of my loop.

And what could cause this behaviour for future reference ?

 

TL:DR how can I make my loop go to the end without the need of interaction through a messagebox.

 

 

  For  BreakCount As Integer = 0 To cmCollection.Count -2 Step 1
       		MsgBox(BreakCount) 'without this the loop ends after 1 break
		BreakBetweenCentermarks(ThisDoc.Document.ActiveSheet.DrawingViews(1), cmCollection.Item(BreakCount), cmCollection.Item(BreakCount + 1))
                
    Next BreakCount

 

Many thanks in advance

 

0 Likes
Accepted solutions (1)
128 Views
2 Replies
Replies (2)
Message 2 of 3

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @loicLPQZG,

It's likely just a timing issue. The message box it providing a pause for the loop to finish processing.
You can try one of these.
Hope that helps,
Curtis

 

add a DoEvents line to allow it to process

For BreakCount As Integer = 0 To cmCollection.Count - 2 Step 1
    Application.DoEvents() ' Allow the application to process pending events
    BreakBetweenCentermarks(ThisDoc.Document.ActiveSheet.DrawingViews(1), cmCollection.Item(BreakCount), cmCollection.Item(BreakCount + 1))
Next BreakCount

 

add a pause/delay

For BreakCount As Integer = 0 To cmCollection.Count - 2 Step 1
    System.Threading.Thread.Sleep(500) ' half second delay
    BreakBetweenCentermarks(ThisDoc.Document.ActiveSheet.DrawingViews(1), cmCollection.Item(BreakCount), cmCollection.Item(BreakCount + 1))
Next BreakCount

 

EESignature

Message 3 of 3

loicLPQZG
Contributor
Contributor

This solved my issue, thank you very much.

 

 

for others i did have to put :

System.Windows.Forms.Application.DoEvents()

 Because the duality of 'Application'.