Solved

Pause a vb application

Posted on 2000-03-22
10
205 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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