943,574 Members | Top Members by Rank

Ad:
Mar 4th, 2009
0

Exiting a sub procedure ?

Expand Post »
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.
Similar Threads
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
RahulV is offline Offline
92 posts
since Jun 2007
Mar 4th, 2009
0

Re: Exiting a sub procedure ?

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.
Reputation Points: 15
Solved Threads: 2
Newbie Poster
SlyMaelstrom is offline Offline
21 posts
since Oct 2005
Mar 4th, 2009
0

Re: Exiting a sub procedure ?

If you don't want it executed, don't call it from the click event.
Reputation Points: 156
Solved Threads: 296
Posting Virtuoso
vb5prgrmr is offline Offline
1,670 posts
since Mar 2009
Mar 4th, 2009
0

Re: Exiting a sub procedure ?

Click to Expand / Collapse  Quote originally posted by vb5prgrmr ...
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.
Reputation Points: 15
Solved Threads: 2
Newbie Poster
SlyMaelstrom is offline Offline
21 posts
since Oct 2005
Mar 5th, 2009
0

Re: Exiting a sub procedure ?

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.
Reputation Points: 156
Solved Threads: 296
Posting Virtuoso
vb5prgrmr is offline Offline
1,670 posts
since Mar 2009
Mar 6th, 2009
0

Re: Exiting a sub procedure ?

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.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
RahulV is offline Offline
92 posts
since Jun 2007
Mar 6th, 2009
0

Re: Exiting a sub procedure ?

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.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
RahulV is offline Offline
92 posts
since Jun 2007
Mar 6th, 2009
0

Re: Exiting a sub procedure ?

Click to Expand / Collapse  Quote originally posted by RahulV ...
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.
Reputation Points: 15
Solved Threads: 2
Newbie Poster
SlyMaelstrom is offline Offline
21 posts
since Oct 2005
Mar 6th, 2009
0

Re: Exiting a sub procedure ?

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.

Quote ...
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.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: How to open a Word Document in Read-only mode
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Popup menu icon on left(help ls)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC