Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 136875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:01:03+00:00 2026-05-11T07:01:03+00:00

(I’m using VB6 but I imagine this comes up in most other languages.) I’ve

  • 0

(I’m using VB6 but I imagine this comes up in most other languages.)

I’ve got a GUI button that calls a routine that takes a minute or two to complete. I want impatient users to be able to click on the button a second time to have it gracefully exit out of the routine at any point.

I used a static variable to make this work pretty well (see code below), but I’m cleaning up the project and I want to put the For/Next loop into its own function, since it’s required in several different places in the project.

But doing that would break my static flag embedded in the for/next, so I need to make some changes. Before I do something hare-brained with public (global) variables, I thought I’d ask what other (smarter, perhaps actually CS educated) people have done when faced with this problem.

So basically my question is how do I replicate this:

Private Sub DoSomething_Click()    Static ExitThisSub As Boolean ' Needed for graceful exit    If DoSomething.Caption = 'Click To Stop Doing Something' Then     ExitThisSub = False ' this is the first time we've entered this sub   Else ' We've re-entered this routine (user clicked on button to stop it)     ExitThisSub = True ' Set this so we'll see it when we exit this re-entry     Exit Sub '   End If     DoSomething.Caption = 'Click To Stop Doing Something'    For i = 0 To ReallyBigNumber     Call DoingSomethingSomewhatTimeConsuming     If ExitThisSub = True Then GoTo ExitThisSubNow     DoEvents   Next    ' The next line was missing from my original example,   ' prompting appropriate comments   DoSomething.Caption = 'Click To Do Something'    Exit Sub  ExitThisSubNow:    ExitThisSub = False ' clear this so we can reenter later   DoSomething.Caption = 'Click To Do Something'  End Sub 

When I move the for/next loop to its own function?

I’m thinking I’ll change ExitThisSub to a public variable QuitDoingSoManyLongCalculations that will exit the new for/next sub and then the DoSomething_Click in the same way.

But I always feel like an amateur (which I am) when I use global variables – is there a more elegant solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:01:04+00:00Added an answer on May 11, 2026 at 7:01 am

    Well you could declare the variable at module level in the forms as private. That is not a global but a module level variable. Then you could pass it to the function you create and check it in the function.

    But be careful with the DoEvents. It basically means allow the windows message loop to process messages. This means that not only can the user click your button again, they can close the form and do other things. So when you are in this loop you’ll need to set a module level variable anyway as you’ll need to check for it in a QueryUnload of the form and in any event handlers.

    You can also use the Tag property of the control itself to store a flag of sorts. But I don’t consider that more elegant.

    I also prefer to use two different buttons. Just hide one and show the other. That way your cancel code and run code are separated in different event handlers.

    To expand on my answer here is some sample code that handles the unloading aspect. Here if you stop via the x it prompts you. If you kill via task manager, it dies gracefully.

    Option Explicit  Private Enum StopFlag    NotSet = 0    StopNow = 1    StopExit = 2 End Enum  Private m_lngStopFlag As StopFlag Private m_blnProcessing As Boolean  Private Sub cmdGo_Click()     Dim lngIndex As Long    Dim strTemp As String     m_lngStopFlag = StopFlag.NotSet    m_blnProcessing = True     cmdStop.Visible = True    cmdGo.Visible = False     For lngIndex = 1 To 99999999        ' check stop flag       Select Case m_lngStopFlag           Case StopFlag.StopNow              MsgBox 'Stopping - Last Number Was ' & strTemp             Exit For           Case StopFlag.StopExit              m_blnProcessing = False             End        End Select        ' do your processing       strTemp = CStr(lngIndex)        ' let message loop process messages       DoEvents     Next lngIndex     m_lngStopFlag = StopFlag.NotSet    m_blnProcessing = False    cmdGo.Visible = True    cmdStop.Visible = False  End Sub  Private Sub cmdStop_Click()     m_lngStopFlag = StopFlag.StopNow  End Sub  Private Sub Form_Load()     m_blnProcessing = False  End Sub  Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)     Select Case UnloadMode        Case vbFormControlMenu, vbFormCode           If m_blnProcessing Then              Cancel = True              If MsgBox('Unload Attempted - Cancel Running Process?', vbOKCancel + vbDefaultButton1 + vbQuestion, 'Test') = vbOK Then                 m_lngStopFlag = StopFlag.StopExit              End If           End If        Case Else           m_lngStopFlag = StopFlag.StopExit          Cancel = True     End Select  End Sub 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer From the examples you gave, I think you're actually asking… May 11, 2026 at 1:42 pm
  • added an answer Well, looks like they are switching from relative positioning to… May 11, 2026 at 1:42 pm
  • added an answer You can generate a DataTable filter string to select rows… May 11, 2026 at 1:42 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.