?
Solved

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
8
Medium Priority
?
801 Views
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)

thanks

Mike

0
Comment
Question by:PHOTOSONTHESPOT
8 Comments
 
LVL 11

Expert Comment

by:leclairm
ID: 16712166
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
 

Author Comment

by:PHOTOSONTHESPOT
ID: 16712538
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16712772
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16716554
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16716557
You could try this if your reference the object.

Set mtimer = nothing
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 1000 total points
ID: 16716648
'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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16717303
1. make sure you don't have doevent inside the timer routine

2. worse case, use End (not recommended)
0
 

Author Comment

by:PHOTOSONTHESPOT
ID: 16719238
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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