954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

STOP a process in between .....

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?

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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 !!!

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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
selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

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...

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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 thecmdButton (to click).

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 
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.

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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?

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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. NowTry 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
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

@ 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?

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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-procedurePublic 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.)

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 
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 theGoTo 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 !!! :'(

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 
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.

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

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-procedurePublic 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.)

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You