Solved

Pause a vb application

Posted on 2000-03-22
10
202 Views
Last Modified: 2010-05-02
I got a vb application with a stop command button, this button must temporailly stop the execution of the application and this even if the application is in a while loop. The application will ask the user if he really want to stop it or to continue it at the same place he was before like the resume on a error check.
0
Comment
Question by:Francois_P
10 Comments
 

Author Comment

by:Francois_P
ID: 2646021
don't answer "doevents" or to add a timer I've already tried those
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2646038
What you need to do is define a public or global variable... For example:

   Public StopSwitch As Boolean

Then in your loops, you can do something like this:

   Do
      'Do some processing here...
      If StopSwitch = True then Exit Do
      DoEvents
   Loop

In your button, you then do something like this:

   If MsgBox("Stop Process?", vbQuestion + vbYesNo) = vbYes Then
       StopSwitch = True
   End If


You are doing to have to sprinkle DoEvents through your program so the program will process the button click.


A WORD OF WARNING... Once you start playing with DoEvents, you are going to have to make sure that you don't allow the user to mistakenly enter a loop twice... For example, Let's say you have a button that starts a process called Command1 and you have a button that stops the process called Command2.

When the user clicks the Command1 button, this is what you would do:

   Command1.Enabled = False
   Do
      'Do some processing here...
      If StopSwitch = True then Exit Do
      DoEvents
   Loop
   Command1.Enabled = True

This stops the user from accidently clicking the Command1 button twice and having the loop kick off twice...


Hope this helps!


Cheers!®©

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2646049
You said >>don't answer "doevents"

Sorry, but DoEvents is the only way to allow the program to dispatch other events while an event is running....


Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2646085
Here is a complete example...

1) Start a new project.

2) Add 2 commandbuttons and a label to the form.

3) Add the following code to the Declarations Section of the form, then run the program and click the "Start" button... Then click the "Stop" button...

    Dim StopSwitch As Boolean
    Private Sub Command1_Click()
        Command1.Enabled = False
        StopSwitch = False
        Do
            DoEvents
            Label1.Caption = CStr(Now)
            Label1.Refresh
            DoEvents
            If StopSwitch = True Then Exit Do
        Loop
        Command1.Enabled = True
    End Sub
    Private Sub Command2_Click()
        If MsgBox("Stop Process?", vbQuestion + vbYesNo) = vbYes Then
            StopSwitch = True
        End If
    End Sub
    Private Sub Form_Load()
        Command1.Caption = "Start"
        Command2.Caption = "Stop"
    End Sub



Cheers!®©
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2646237
mcrider is correct - you will have to use DoEvents at least occasionally, so that the program will respond to the button click.

If DoEvents on every loop slows your program down too much, then you can do it once every 10, 100, 1000 tries

Sub Do_A_Lot()

Dim DoCount as Integer
Const DoLoopLimit = 100

Do
    HeavyProcessingInLoop
    DoCount=DoCount+1
    If DoCount = DoLoopLimit Then
        DoCount = 0
        DoEvents
    End If
Loop Until Done
End Sub

Any message box being displayed by your program will suspend processing for your program until it is replied to, so a MsgBox (as mcrider used) is almost certainly your best bet.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Expert Comment

by:pclement
ID: 2646258
As mcrider mentioned you need to use DoEvents unless you want to lock up the thread on which your application runs.

You can use the Sleep API function, which will lock the application thread:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Suspends app for one second
Call Sleep(1000)

or you can use SetWaitableTimer:

http://support.microsoft.com/support/kb/articles/Q231/2/98.ASP

0
 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
ID: 2646347
pclement,

This is an direct qoute from microsoft:

HOWTO: Determine the Differences Between DoEvents and Sleep
http://support.microsoft.com/support/kb/articles/Q158/1/75.asp

 "DoEvents is a Visual Basic function that yields execution so the operating system can process other events. This function cleans out the message loop and executes any other pending business in the Visual Basic runtime. Upon completing the pending business execution, the function calls the Sleep function with zero (0) as the argument so that the remaining time slice can be used to check the queue.

The Sleep 32-bit API function is a subset of the DoEvents function. The Visual Basic program calling the function and the Visual Basic runtime executable and interactions with Windows are immediately put to sleep by this function. The programs remain inactive for the time in milliseconds specified in the Sleep argument."



Since Francois_P wants to process a click event on a button to stop the process running, sleep will not work...


Cheers!®©
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2646432
When I'm in a big loop I insert a call to a function that checks the keystate of the Escape key.  Then I know if someone has pressed the escape key (or is holding it down and cursing at the machine) and can pause to ask them if they want to keep going:

API declaration:

Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Function:

Sub EscapeNow()
DoEvents
If GetKeyState(vbKeyEscape) < 0 Then
        If MsgBox("Would you like to terminate the operation?", vbYesNo, "Cancel Operation") = 6 Then
            End
        End If
End If
End Sub

Then just call EscapeNow() in your loop:

Do
'your program events
EscapeNow()
Loop

and you'll check the status of the escape key and run a doevents.  You can also only run it every 10th or 100th loop as bhess1 has noted.

The problem is that my function doesn't do anything to clean up the environment if the user ops to quit.  It uses the dreaded End statement, which every good programmer knows should be left out of their code.  You'll need to make sure you're setting object variables to nothing and clearing critical stuff from memory.

Wes
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2646603
Thanks for the points! Glad I could help!


Cheers!®©
0
 
LVL 1

Expert Comment

by:pclement
ID: 2646703
mcrider:

>Since Francois_P wants to process a click event on a button to stop the process running, sleep will not work...<

Correct. As I mentioned Sleep will lock the application thread for the specified amount of time preventing him from interracting with the application.

Not what he wanted.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now