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)?

Posted on 2006-05-18
Last Modified: 2008-01-09
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)



    LVL 11

    Expert Comment

    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??

    Author Comment

    you mean something like this

    sub timerupdate_timer()

    do while somethingwonderful
       if not forminqustion.visible then exit do

    ...other stuff

    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.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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?
    LVL 29

    Expert Comment

    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
    LVL 29

    Expert Comment

    You could try this if your reference the object.

    Set mtimer = nothing
    LVL 29

    Accepted Solution

    'Add 2 command buttons and 1 Timer to Form1

    Dim thread As Class1

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

    Private Sub Command2_Click()
    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
        Next I
    End Sub
    LVL 26

    Expert Comment

    1. make sure you don't have doevent inside the timer routine

    2. worse case, use End (not recommended)

    Author Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now