Exiting a sub procedure ?

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Exiting a sub procedure ?

 
0
  #1
Mar 4th, 2009
Consider this VB6 source code

visualbasic Syntax (Toggle Plain Text)
  1. Public bc As Boolean
  2. Dim x As Long
  3.  
  4. Private Sub Command1_Click()
  5. If bc Then
  6. MsgBox x
  7. Exit Sub
  8. Else
  9. Call exec
  10. End If
  11. End Sub
  12.  
  13. Private Sub Command2_Click()
  14. bc = True
  15. Call Command1_Click
  16. End Sub
  17.  
  18. Private Sub Form_Load()
  19. bc = False
  20. End Sub
  21.  
  22. Public Sub exec()
  23. While 1 = 1
  24. x = x + 10
  25. Label1 = x
  26. DoEvents
  27. Wend
  28. End Sub

now here, i want to stop the execution of a sub-procedure Public Sub exec() which is called by another sub-procedure Private Sub Command1_Click() but without changing or adding any code into Public Sub exec().

Does anyone have an answer?
How to do it?
Last edited by RahulV; Mar 4th, 2009 at 12:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 21
Reputation: SlyMaelstrom is an unknown quantity at this point 
Solved Threads: 2
SlyMaelstrom's Avatar
SlyMaelstrom SlyMaelstrom is offline Offline
Newbie Poster

Re: Exiting a sub procedure ?

 
0
  #2
Mar 4th, 2009
You're asking to make code do something different without changing it. It's kind of difficult to do.

I would say, if you wanted to halt execution on the sub-routine before it completes without changing the code in the sub-routine, then you'd have to call it asynchronously in a new thread and have your original thread halt execution when a condition is met (like Labell equalling a specific value) by killing your new thread prematurely. Would that cause a memory leak? Almost definitely, but, it meets your criteria. Besides that, I can't think of a way to do what you're describing. I would highly, highly recommend that you don't do that, though.

I'd have to ask why you're calling a sub-routine that goes into an infinite loop, anyway. I'd also have to ask why you insist that it can't be changed to suit your needs.
Last edited by SlyMaelstrom; Mar 4th, 2009 at 2:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 914
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 167
vb5prgrmr vb5prgrmr is offline Offline
Posting Shark

Re: Exiting a sub procedure ?

 
0
  #3
Mar 4th, 2009
If you don't want it executed, don't call it from the click event.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 21
Reputation: SlyMaelstrom is an unknown quantity at this point 
Solved Threads: 2
SlyMaelstrom's Avatar
SlyMaelstrom SlyMaelstrom is offline Offline
Newbie Poster

Re: Exiting a sub procedure ?

 
0
  #4
Mar 4th, 2009
Originally Posted by vb5prgrmr View Post
If you don't want it executed, don't call it from the click event.
Do you think he is looking to avoid executing it, at all? I didn't even consider that's what he was asking. He said stop execution and I saw that exec() continued in an infinite loop, so I assumed he was looking for a way to halt execution at a point.

We'll see what he says, I guess.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 914
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 167
vb5prgrmr vb5prgrmr is offline Offline
Posting Shark

Re: Exiting a sub procedure ?

 
0
  #5
Mar 5th, 2009
Originally Posted by SlyMaelstrom View Post
Do you think he is looking to avoid executing it, at all? I didn't even consider that's what he was asking. He said stop execution and I saw that exec() continued in an infinite loop, so I assumed he was looking for a way to halt execution at a point.

We'll see what he says, I guess.
You could be right, but he also stated that he did not want to alter the exec sub, which meant to me that it is called from someplace else and while I agree it does look like an infinant loop that only attempts to update a label with a value like a progress indicator, one would think that there is a way to exit the sub even if we don't see it from the example given. Which leads me to believe that we don't have the entire picture of what is going on.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Exiting a sub procedure ?

 
0
  #6
Mar 6th, 2009
Originally Posted by SlyMaelstrom View Post
You're asking to make code do something different without changing it. It's kind of difficult to do.

I would say, if you wanted to halt execution on the sub-routine before it completes without changing the code in the sub-routine, then you'd have to call it asynchronously in a new thread and have your original thread halt execution when a condition is met (like Labell equalling a specific value) by killing your new thread prematurely. Would that cause a memory leak? Almost definitely, but, it meets your criteria. Besides that, I can't think of a way to do what you're describing. I would highly, highly recommend that you don't do that, though.

I'd have to ask why you're calling a sub-routine that goes into an infinite loop, anyway. I'd also have to ask why you insist that it can't be changed to suit your needs.
Thank you,
I too had considered the Thread concept but I dont know how to implement Thread in VB 6.

Can you help me how to create and implement Threads? I think that would work.

Any other techniques to perform the required would be highly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Exiting a sub procedure ?

 
0
  #7
Mar 6th, 2009
Originally Posted by SlyMaelstrom View Post
Do you think he is looking to avoid executing it, at all? I didn't even consider that's what he was asking. He said stop execution and I saw that exec() continued in an infinite loop, so I assumed he was looking for a way to halt execution at a point.

We'll see what he says, I guess.
Yes you are right.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 21
Reputation: SlyMaelstrom is an unknown quantity at this point 
Solved Threads: 2
SlyMaelstrom's Avatar
SlyMaelstrom SlyMaelstrom is offline Offline
Newbie Poster

Re: Exiting a sub procedure ?

 
0
  #8
Mar 6th, 2009
Originally Posted by RahulV View Post
Thank you,
I too had considered the Thread concept but I dont know how to implement Thread in VB 6.

Can you help me how to create and implement Threads? I think that would work.

Any other techniques to perform the required would be highly appreciated.
Google it.

There is way too much to be said about multi-threading to write in a reply to this thread and I don't have the time or patience to write it all. Not to mention, I wouldn't want the opportunity to miss something and give you bad info. As I said in the first place, it's a very bad idea to kill a thread prematurely. It basically has the same effects of running a macro in some scripting language and clicking the kill button before it finishes. You leak file buffers, you leak stack frames. It's just a big mess and represents bad programming practice. I only said it because it is the only way I can think of doing what you feel you need. However, from my experience, I'd say you probably don't need what you think you need. Step back and take another look at it. There is a better solution there.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Exiting a sub procedure ?

 
0
  #9
Mar 6th, 2009
If you want to run another thread in VB6 that's independent of your main program, you should write an ActiveX exe file that will take on its own independent thread.

Microsoft in their MSDN documentation recommends using an out of process ActiveX exe.

The fact that an out-of-process component runs in its own process means that a client can tell it to do something, and then go about its business while the component does the work. When such a system is properly set up, the component can tell the client when the task is done using an asynchronous notification, as explained in "Asynchronous Call-Backs and Events" in "Building Code Components."
These aren't really that hard to program. But you might want to go through their examples found in the MSDN CD's or on the archives for MSDN on the internet.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 803 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC