Link to home
Start Free TrialLog in
Avatar of PHOTOSONTHESPOT
PHOTOSONTHESPOTFlag for Canada

asked on

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

Avatar of leclairm
leclairm

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??
Avatar of PHOTOSONTHESPOT

ASKER

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
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?
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
You could try this if your reference the object.

Set mtimer = nothing
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1. make sure you don't have doevent inside the timer routine

2. worse case, use End (not recommended)
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