Consider this VB6 source code

Public bc As Boolean
Dim x As Long

Private Sub Command1_Click()
     If bc Then
          MsgBox x
          Exit Sub
     Else
          Call exec
     End If
End Sub

Private Sub Command2_Click()
     bc = True
     Call Command1_Click
End Sub

Private Sub Form_Load()
     bc = False
End Sub

Public Sub exec()
     While 1 = 1
          x = x + 10
          Label1 = x
          DoEvents
     Wend
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?

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.

If you don't want it executed, don't call it from the click event.

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.

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.

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.

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.