In VB6 - How to unload a form that is in the middle of having code executed on it due to a multi-threaded programming model (using timers)?

I have a program which there is a "CANCEL" button on a form which is supposed to  unload the form in question and reactivate the form that called it.  The problem is that a timer triggered event is updating picture boxes and while that occurs, this form (the one with the cancel button and picturboxes) will reappear even though I tried to  unload it.  How do I stop execution dead on a form before unloading it.  ?  Is this clear enough?

I tried to code an 'abort' global variable which I would set true when I'm about to unload the form (and the code in the timer would exit its loop if abort is set true (if abort then exit sub).  Problem is that  mostly this works but sometimes due to some timing constraint .. the form will disappear and reappear with a picturebox being updated nonetheless.  Debugging shows the abort value to be true in all cases so I'm sure it's not a coding error (too simple to be coding error)

Please advise (p.s. I'm not  a novice but programming multi-threaded apps in vb6 is something I'm not used to)

thanks

Mike

PHOTOSONTHESPOTAsked:
Who is Participating?
 
nffvrxqgrcfqvvcCommented:
'Form1
'Add 2 command buttons and 1 Timer to Form1

Dim thread As Class1

Private Sub Command1_Click()
    Set thread = New Class1
    thread.Begin
End Sub

Private Sub Command2_Click()
    thread.ExitThread
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set thread = Nothing
End Sub


'--- class1 ---

Option Explicit

Dim mForm   As Form
Dim WithEvents mTimer  As Timer

Public Sub Begin()
   
    mTimer.Enabled = True
    mTimer.Interval = 500
    'Only call thread once.
    mTimer.Interval = 0
   
End Sub

Public Sub ExitThread()
    Set mTimer = Nothing
End Sub

Private Sub Class_Initialize()
    Set mForm = New Form1
    Set mTimer = Form1.Timer1
End Sub

Private Sub Class_Terminate()
    Set mTimer = Nothing
    Set mForm = Nothing
End Sub

Private Sub mTimer_Timer()
    'Do your processing here..
    Dim I As Long
   
    For I = 0 To 2000
        Debug.Print I
        If mTimer Is Nothing Then
            Exit Sub
        End If
        DoEvents
    Next I
   
End Sub
0
 
leclairmCommented:
If it's in its own thread, why not just hide the form and put the unload code in the event that the timer triggered??
0
 
PHOTOSONTHESPOTAuthor Commented:
you mean something like this

sub timerupdate_timer()

do while somethingwonderful
   if not forminqustion.visible then exit do

...other stuff


loop
if not forminquestion.visible then unload me
end sub


would this work?  



Leaving the form hidden (and having the thread run) takes up a lot of computing power.  It's a high-res graphical app.

Mike
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think you're on the right track already.  Disable the Timer and set your global variable.  Then EVERYWHERE in the Timer event you need to check the globabl before you manipulate any controls on the form.  You may have to spatter the code with multiple checks.

May we see the Timer code?
0
 
nffvrxqgrcfqvvcCommented:
if the process is in the middle of a task then you won't be able to exit until all the code in the Timer procedure is finished. This is what idle_mind means by "You may have to spatter the code with multiple checks"
simply placing it at the top will stop it for the next time around, but won't stop it while its processing therefor you will have multiple checks going all the way down to the last line of your routine
0
 
nffvrxqgrcfqvvcCommented:
You could try this if your reference the object.

Set mtimer = nothing
0
 
EDDYKTCommented:
1. make sure you don't have doevent inside the timer routine

2. worse case, use End (not recommended)
0
 
PHOTOSONTHESPOTAuthor Commented:
Thank you one and all for your input.  The threading code is very similar to my actual structure so that's what I've tried ... and it worked!!  

Now I also see a couple of other ways to do this.  I really appreciate the help

Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.