hi,
i've created a Form1 which contains a long running process. But i want an feature that will help the user to stop the running process in between, when i click a Button or some other control.

How is it possible?

Recommended Answers

All 16 Replies

Also the problem is that my Form1 which runs the process goes like disabled (automatically), ..... thats because, .... u can imagine because its a long process.

so im not able access the controls on the Form1.


PLS help !!!

Hi, use

DoEvents function so that other process get chance. This function helps to disable your form.

Example

Dim bStop As Boolean

Private Sub cmdProcess_Click()
   Do While bStop <> True
      '
      '  Long Process
      '
      CurrentX = 0
      CurrentY = 0
      Cls
      Print Time
      DoEvents
   Loop
   bStop = False
End Sub

Private Sub cmdStop_Click()
   bStop = True
End Sub

If it is a system call (such as shell, or wsh.run) and it is blocking (ie, the program is waiting for shell or wsh.run to finish the externally running program), you need a much more complicated method involving things like waitforsingleobject...

Hi, use

DoEvents function so that other process get chance. This function helps to disable your form.

Example

Dim bStop As Boolean

Private Sub cmdProcess_Click()
   Do While bStop <> True
      '
      '  Long Process
      '
      CurrentX = 0
      CurrentY = 0
      Cls
      Print Time
      DoEvents
   Loop
   bStop = False
End Sub

Private Sub cmdStop_Click()
   bStop = True
End Sub

Sorry this did not work i told u that, mu Form1 gets disabled, thats normal when there's some long process is taking place. So im not getting access to the cmdButton (to click).

If it is a system call (such as shell, or wsh.run) and it is blocking (ie, the program is waiting for shell or wsh.run to finish the externally running program), you need a much more complicated method involving things like waitforsingleobject...

Im not running any other program or shell.
Its just that the Form1 is processing a long process. Like suppose counting to 1000crores or more.

then doevents should work just fine.... somewhere when your form1 is doing processing... probably in some kind of do loop or while loop or for loop, simply stick a doevents.

for i = 0 to 1000000
     retval = i * i
     doevents
next i

then doevents should work just fine.... somewhere when your form1 is doing processing... probably in some kind of do loop or while loop or for loop, simply stick a doevents.

for i = 0 to 1000000
     retval = i * i
     doevents
next i

Sorry it did not work for me.....
Pls can anyone help me with this.

can creating thread help with this?

I'm going to need to see your code then, in order to be able to troubleshoot this further.
You can not create a thread in Visual Basic 4/5/6. Doevents is only a partial fix to your problem. Doevents tells VB that a long ass loop is going on, and that it needs to do other stuff too, like handle mouse clicks. So, just for example, make a form and put 2 buttons on it....in button1, put this:

while 1 = 1
     x = 200 * 200
wend

Then, in button2, put this:

end

Now run the code, and click button1. Now Try to click button2. Nice Huh? Now modify button1 to have this:

while 1 = 1
     x = 200 * 200
     doevents
wend

Run the code, click button1, now click button2..... oh wow. Big difference.

@ Comatose

Okay!
that does work.

but in ur example u had written

End

statement for Button2 and that ends the whole program, instead i want to just end the running sub-procedure or the loop.

You have any other idea on this?

Uh, yeah. Instead of "end", have a global Boolean variable. Have it set to like false.... then in button2, have it says something like flag = true , then in button1, have an if or something like if flag = true then exit sub . Or something.

Consider this code:

Public bc As Boolean
Dim x As Long

Private Sub Command1_Click()
     If bc Then
          MsgBox x
          GoTo EndHere
     Else
          Call exec
     End If
EndHere:
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().
even after applying all ur helps it did not work as needed. But all your help was valuable.

How to do it?

(Forgive me for not providing the code earlier.)

It's a very bad practice to use goto in programming. It is strongly frowned upon by professional programmers, and actually forbidden in business code. Instead, you can use "exit sub" to replace that goto statement. Even that (since it is just a single if statement and nothing else) isn't even necessary, as the code will simply end once the if statement is completed.

It's a very bad practice to use goto in programming. It is strongly frowned upon by professional programmers, and actually forbidden in business code. Instead, you can use "exit sub" to replace that goto statement. Even that (since it is just a single if statement and nothing else) isn't even necessary, as the code will simply end once the if statement is completed.

But if i replace the GoTo statement with End Sub, it give a compiler error "Block If without End If". So i had to use the GoTo statement.

Now pls can anyone help me in solving the basic problem being discussed here?

PLEASE HELP !!! :'(

Not end sub.... exit sub...

Not end sub.... exit sub...

O!
i'm terribly sorry for that!!
(What's happening to me these days? ...... might be that im in high tension to know more!!!)
once again i'm sorry!!

Pls help me with the main problem!! that we are discussing here.

ONCE AGAIN REPEATING THE PROBLEM

Consider this code:

Public bc As Boolean
Dim x As Long

Private Sub Command1_Click()
     If bc Then
          MsgBox x
          GoTo EndHere
     Else
          Call exec
     End If
EndHere:
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().
even after applying all ur helps it did not work as needed. But all your help was valuable.

How to do it?

(Forgive me for not providing the code earlier.)

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.