Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For loop and subroutine bad sync

Status
Not open for further replies.

bpcampbell

Programmer
Apr 22, 2003
3
US
I have a timer that contains a For loop that reads the contents of each row of an MSFlexGrid, and uses those contents in a subroutine to poll an SNMP agent. It is my intention to write the returned value to a field in the MSFLexGrid.

When running, everything works as it should, except the for loop continues before the subroutine is finished, resulting in an incorrect value for the returned variable. How can I syncronize the process and make the for loop wait until the sub has completed?

I hope I am clear in the problem I describe. I have used several Watches and Debug.Print statements to isolate this problem and hope someone can help me.

Brian
 
Without seeing the code, it would be my guess that it's the timer that's actually the source of the problem.

You might try disabling the timer before you start your subroutine, and then re-enable it after it is finished.

You don't have any "DoEvents" statements anywhere in your loop or subroutine, do you?

Robert
 
I don't think it is the timer. I tested the same code with a button and it did not return right away and when it finally did, it was not a correct value -- the same behavior I am experiencing with the timer. I think the problem lies in the For loop and the subroutine, in this case snmpSend.

For what it's worth, the timer code is below. The snmpSend sub code is too extensive to post here.

Private Sub Timer1_Timer()
For K = 1 To MSFlexGrid3.Rows - 1
sIPAddress = MSFlexGrid3.TextMatrix(K, 1)
sCommunity = MSFlexGrid3.TextMatrix(K, 3)
sTempOID = MSFlexGrid3.TextMatrix(K, 25)
snmpSend sIPAddress, sCommunity, sTempOID, pduGet
MSFlexGrid2.TextMatrix(K, 4) = OutPut
Next K
End Sub
 
In VB code, if you execute a subroutine from inside a loop, that sub should finish before execution is returned back to the loop.

If you used a DoEvents statement in your subroutine, then that *could* cause the execution to jump back to the For/Next loop and start all over again. But since you tested it with a button and not a timer, this is probably not the case.

If you single step ( F8 ) through the code, does it operate correctly?

Just as a test, have you tried running the code without looping through all of the rows of the grid, ( in other words, test it by just running one row and not using a loop. ) and seeing if the returned value is correct for that row?

Robert
 
I worked around it. I am not sure why it is happening but my solution is to pass a variable with the FlexGrid's row to the subroutine and write the SNMP value to the FlexGrid from within the sub. Thanks for the help, Vampire, I may figure out the real reason for the problem one day.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top