|
-
Nov 4th, 2000, 04:56 PM
#1
Thread Starter
Lively Member
I have an Exit button that Unloads the form and a long series of calculations and DoEvents. When the Exit button is clicked, the form uloads but the code continues to execute and causes an error (obviously the vars, arrays, etc. no longer exist!). Microsoft says to use "Set form1 = Nothing" but this doesn't appear to do anything.
Is there a way to get the code to really terminate?
Jon
-
Nov 4th, 2000, 05:11 PM
#2
Monday Morning Lunatic
Try using End. However, this is definitely not the best method. Really, you should rethink your calculation model, and have it occasionally check some kind of status flag to see if it should continue, since terminating the process like that can really screw things up .
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 4th, 2000, 05:13 PM
#3
transcendental analytic
No, don't! Don't use End. Instead check which doevents are executing the moment you close your form, press ctrl break for instance.
Then if it's a loop, with a single doevents, add while forms.count to the end or beginning of the loop
If it's not, change the statement to:
IF forms.count then doevents
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 4th, 2000, 09:08 PM
#4
End Terminates execution. Never required by itself but may be placed anywhere in a procedure to close files opened with the Open statement and to clear variables.
-VB4 Help File
Use:
Unload Me
Set Form1 = Nothing
Code:
Public Sub UnloadAllForms()
Dim Form As Form
For Each Form In Forms
Unload Form
Set Form = Nothing
Next Form
End Sub
-
Nov 5th, 2000, 03:51 AM
#5
Thread Starter
Lively Member
Thanks for all the help.
Once again, I found something 'interesting' about VB. Try creating a small program with 1 Form, a looped calcalation and an Unload statement:
x = 1
Do While x < 50000
x = x + 1
Unload Form1
Set Form1 = Nothing
Loop
This little program will unload its form AND try to continue to execute which will cause errors! MSDN Online even says so!
Insert 1 line after the Unload statement:
If Forms.Count < 1 Then Exit Sub
and the code works fine!
Your wondering why such code? I've got a small game with a lot of calculations. The player might need to exit the prg during the calcs. I have an Exit button that Unloads the form and these 2 lines in the calcs:
DoEvents
If Forms.Count < 1 Then Exit Sub
Works great! Thanks for your help,
Jon
-
Nov 5th, 2000, 09:50 PM
#6
transcendental analytic
uh, i thought this was my idea well well i didnt' look in MSDN but found out myself, anyway this seems to be a big problem since i've told ppl over and over again and it seems like it has to be put as a tip on vb-world or something so that everyone can see that this is so.
BTW, that piece of code will work, did you copy it from MSDN? Since it's the Doevents, the first doevents after the form is unloaded, that causes the hanging up.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 6th, 2000, 11:08 AM
#7
Thread Starter
Lively Member
Sorry Kedaman, I did get the idea from YOU and not from MSDN! I should have given you credit for the suggestion.
Before I received your idea I did search MSDN for info on the Unload statement and discovered that it doesn't terminate code if it is called from within a sub by DoEvents. I like to know as much as possible about what's going on in my code!
Thanks for the suggestion,
Jon
-
Nov 6th, 2000, 02:16 PM
#8
transcendental analytic
I mean i thought this was really something that M$ didn't know about Still it seems like nobody really knows.
It's not the unload call, it's not it at all that causes it, it's the doevents. If that's what M$ states, then it's wrong.
Think about subclassing, when you close the window before it's terminated, it will crash. Same is for doevents, if you use doevents where as there is no window to recieve the events, it crashes.
Now this goes for exactly every single doevents in the whole app, any of them could cause this, but only those that are placed in frequent or longlasting loops are critical. I suggest if you don't have other use, DON'T USE DOEVENTS. For giving the form a chance to update once is OK, but don't throw it everywhere and think it will run smoother.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 6th, 2000, 09:43 PM
#9
Thread Starter
Lively Member
Hi Kedaman,
Not sure I understand your point but I do know the following code works. Form1 contains two CommandButtons: one to start the calculations and one to close the form.
Dim x As Integer, y As Integer
Private Sub cmdStart_Click()
x = 0
Do While x < 10000
y = 0
Do While y < 10000
y = y + 1
DoEvents
If Forms.Count < 1 Then Exit Sub
Loop
x = x + 1
Loop
End Sub
Private Sub cmdExit_Click()
Unload Form1
Set Form1 = Nothing
End Sub
Without the line "If Forms.Count < 1 Then Exit Sub", the code doesn't work.
Jon
-
Nov 7th, 2000, 08:09 AM
#10
transcendental analytic
Yes, youve got me right, but there's one more thing, doevents is also performance.
Code:
Dim x As Integer, y As Integer
x = 0
Do While x < 10000 Or Forms.Count
y = 0
Do While y < 10000
y = y + 1
Loop
x = x + 1
DoEvents
Loop
Try to place the doevents where it's not launched every millisecond. That would make your app slow as hell if that's not the purpose of course. Now this example is still slowed down by doevents, since it's launched at about 300 times a second. you could make it launch 10 times a second by just ignoring it 29 times of 30:
Code:
Dim x As Integer, y As Integer
x = 0
Do While x < 10000
y = 0
Do While y < 10000
y = y + 1
Loop
x = x + 1
If x Mod 30 = 0 Then DoEvents: If Forms.Count = 0 Then Exit Do
Loop
Now you could ask how i did know how often doevents was triggered? Well i put a debug.print x directly after the doevents and tested some values for the mod operator until it was fired about 10 times a second.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|