Easy 150 points...

joooaaa
joooaaa used Ask the Experts™
on
My code is the following:

Private arrSnoopResults() As String

Private Sub StartSnoops()
Dim numTimes As Integer
numTimes = CInt(txtNumSnoops.Text)
Dim Index As Integer
If numTimes > 0 Then
 ReDim arrSnoopResults(numTimes)
 For Index = 1 To numTimes
   Load tmrSnoop(Index)
   Load Snooper(Index)
 Next
For Index = 1 To numTimes
  tmrSnoop(Index).Interval = (1000+ (100*numTimes))
  Debug.Print "Setting timer " & Index
  tmrSnoop(Index).Enabled = True
Next
End If
End Sub

Private Sub tmrSnoop_Timer(Index As Integer)
Debug.Print "Starting Snoop" & Index
arrSnoopResults(Index) = Snooper(Index).OpenURL("www.google.com")
tmrSnoop(Index).Enabled = False
End Sub

Private Sub Command1_Click()
 Call StartSnoops
End Sub

trmSnoop is the timer w/ index set to 0
Snooper is the inet w/ index set to 0
txtNumSnoops is the textbox for setting the loop variable to a # of times to loop.

I would like to know how to unload trmSnoop and Snooper when the StartSnoop function ends all of its processes...
jbajwa said the following, but i didnt really understand what i had to do:

"The best thing will be to create a Public variable lets say totalObjects
increment this variable from 1 and so on when u are loading objects and start decrementing as you timers get fired. so when the last timer gets fired you value will be back to from where you started and at that time you can safely call unloadobjects to release the memory."

He also provided me this code, before he told me the quotations above:

Private Sub UnloadObjects()
 Dim objToUnload = CInt(txtNumSnoops.Text)
 For index = objToUnload to 1 step -1
  unload tmrSnoop(Index)
  unload Snooper(Index)
 next index
End Sub

So my question is this: How do i call UnloadObject or unload tmrSnoop and Snooper without getting an error of still executing a procedure?

Regards, Joe.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
set a global variable first.  Then test that variable in your looping code to see if it is true.  If true. exit the loop.  If not continue.  Then call the unload.


Private Sub tmrSnoop_Timer(Index As Integer)
  if GlobalUnloading = true then
     tmrSnoop.enabled = false
  exit sub

Debug.Print "Starting Snoop" & Index
arrSnoopResults(Index) = Snooper(Index).OpenURL("www.google.com")
tmrSnoop(Index).Enabled = False
End Sub

Author

Commented:
could you please provide me with the code of how it will accualy look? Because I'm kind of a newb and dont really know how to declare global vars yet... Public GlobalUnloading As Boolean ?
yes.  Put that in a module.  Then before you unload the forms, set the value to True.

Example:

Module1
Public GlobalUnloading as Boolean

YourCodeLocation:

Private Sub UnloadObjects()
GlobalUnloading = True
Dim objToUnload = CInt(txtNumSnoops.Text)
For index = objToUnload to 1 step -1
 unload tmrSnoop(Index)
 unload Snooper(Index)
next index
End Sub

And the timer code I provided earlier

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
well thats not exctly what i needed... you see i want it to unload the timer and inet when it finishes StartSnoops fuction, so after that i could load those objects again...
because i get an error that an object is already loaded if i press the command button again...
so what i need is for the prog to unload timer and inet right after StartSnoops ends without interruption of "still executing last request"... I dont need to unload the forms and with that unload the objects,,, i just need it to first load the timer array and inet array... do its thing then unload the two arrays... but not close the program... tnx
the problem here is that The timers are on a different thread and they could still be running when StartSnoops finishes.  However, have you tried:

'Module Code
m_ColTimersToDelete

Create a single timer  Call it TimerCleanup and set it's interval to say 1000 (every second)
private sub TimerCleanup_Timer
   dim objTimer as object
   TimerCleanup.enabled = false  
   for each objTimer in m_colTimersToDelete
        unload objTimer
   next
   TimerCleanup.enabled = True

end sub

Private Sub tmrSnoop_Timer(Index As Integer)
tmrSnoop(Index).Enabled = False
Debug.Print "Starting Snoop" & Index
arrSnoopResults(Index) = Snooper(Index).OpenURL("www.google.com")
m_ColTimersToDelete.add

End Sub

Private Sub StartSnoops()
....earlier code
For Index = tmrSnoop.ubound + 1 To tmrSnoop.ubound + numTimes
  Load tmrSnoop(Index)
  Load Snooper(Index)
Next
...later code
end sub

Author

Commented:
what is m_ColTimersToDelete anyway ?.. could you please provide how the code would look like in the program(the full Form1 code)...
i need it to load those objects, go to the url, then unload the objects at a push of a button(one button only)

please help and
tnx in advance...

ps - increasing points to 175.
m_colTimersToDelete is a standard VB collection.  We put each timer that we want deleted into this collection.  That way when the timer to destroy all the other timers that have finished kicks in it simply needs to run through that collection.  Here's your code modified:
1) Add a new timer on your form.  Name it tmrCleanup (changed the name to your standards). Set it's interval to 1000 or something.
2) Paste the code into your form.
'General Declarations section
Private m_ColTimersToDelete as vba.collection
Private arrSnoopResults() As String

private sub tmrCleanup_Timer
  dim objTimer as object
  tmrCleanup.enabled = false  
  for each objTimer in m_colTimersToDelete
       unload objTimer
  next
  tmrCleanup.enabled = True

end sub


Private Sub StartSnoops()
Dim numTimes As Integer
numTimes = CInt(txtNumSnoops.Text)
Dim Index As Integer
If numTimes > 0 Then
ReDim arrSnoopResults(numTimes)
For Index = 1 To numTimes
  Load tmrSnoop(Index)
  Load Snooper(Index)
Next
'Assign the new timers with index numbers that have not been used so far or with index numbers that have been used but have been removed so the ubound property works well here.
For Index = tmrSnoop.ubound + 1 To tmrSnoop.ubound + numTimes
 tmrSnoop(Index).Interval = (1000+ (100*numTimes))
 Debug.Print "Setting timer " & Index
 tmrSnoop(Index).Enabled = True
Next
End If
End Sub

Private Sub tmrSnoop_Timer(Index As Integer)
'Moved this line to the top to turn off the timer immediately, but still allow this code to finish
tmrSnoop(Index).Enabled = False
Debug.Print "Starting Snoop" & Index
arrSnoopResults(Index) = Snooper(Index).OpenURL("www.google.com")
'Newly added line to add this timer to the collection of timers to clean up.
m_ColTimersToDelete.add tmrSnoop(Index)

End Sub

Private Sub Command1_Click()
Call StartSnoops
End Sub

Hope this helps you!  Let me know any problems.

Author

Commented:
Hey thanks a lot... You've been a big help...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial