Stuck in infinite loop. Any way to break vba into Code when Control+Break doesn't work - vba

I have code stuck.
It might be in an infinite loop.
Not sure.
Is there a way to break the program to stop at the current line of code that it is running on?
I'd like to avoid shutting down excel because I want to be able to catch where in the loop it is, and by going into the code, I will be able to tell how much processing was done. I would like to break into the code, if possible.
It is stuck on hour glass.
Ctrl+Break doesn't seem to work
Seems like the running code has hijacked all the quota that cpu is giving to excel.
If there is nothing I can do now, is there something in the future I can do to where I can more easily break into the code?
I'm thinking that an intermittent wait within a loop might be a feasible solution.

In the future, include a DoEvents inside the loop. It will run a little slower, but you will be able to use Ctrl+Break to stop it from running.

Create a progress dialog when entering the loop and include a Cancel button. Within your loop check for the Cancel signal/event. This also gives you some flexibility on how you react to the Cancel - you could stop the loop and display key information in a new dialog box (for example).
Basic steps to achieve what I have described (not necessarily the most elegant or the most re-useable, but simple enough for those of lesser experience):
create a modeless (not modal) Form with either suitable labels or a progressbar item (for
visual effect). Include a public property (Boolean) for Cancel (e.g.
boolCancel)
Place a button on form and onClick set boolCancel = True
In your main code, show the form just before your problem loop.
while in your loop you can update some label or progress bar on the
form so that you have a visual indication of whether the loop is
doing something of value or if it is now simply spinning its wheels.
How you do this depends on what your loop is doing.
Also while in your loop check your boolCancel value. If true then
display any state information you want and break from the loop.
If your loop ends normally, hide/unload the progress dialog.

Related

Stop button in LabVIEW cannot be pressed during while loop execution inside an event structure

I am currently working on a LabVIEW project and have found myself stuck on how to make a while loop exit when I press the abort (stop) button. For a simple while loop I understand how to do this - but the problem is that this while loop is nested inside of an event structure and I'm guessing that the button cannot be pressed while the loop is executing. Attached here is a picture of part of my code (that contains this specific event case which is causing me problems): To spend a little more time explaining what the problem is - I think the code is doing what I want it to do (namely output a set of commands in a repeated cycle with a wait timer) but I cannot stop the code mid cycle (pressing the abort button with my mouse does nothing - as in the button doesn't show it being pressed and the indicator shows no change, I also can't use any other functionality of my program during the cycle which I'm assuming is related). And I do not want to stop the LabVIEW program from running entirely - just the code inside the while loop pictured above. This is what the front panel is configured too for completeness:
Essentially what I want to happen is the while loop to execute when I press DWG and in the middle of the cycle be able to abort it. Sorry if my code seems a little messy. Additionally, I've tried the same code with a for loop originally (via a conditional terminal so it could stop early) and that didn't work either. Thanks for any help I appreciate it!
Your problem is that inside the event structure, by default the UI is frozen so no UI actions (keyboard/mouse/etc) are processed until you exit that frame.
Option 1. You can right click the Event Structure and select "Edit events handled by this case" dialog and then uncheck the "Lock panel" checkbox -- that will allow the UI to be live while you are in that frame. I do not recommend this solution generally unless you have an extremely simple user interface because it leads to the user being able to change controls without the events behind those controls being processed (not a good UI experience for users). But if the UI is simple enough, that works.
Option 2. You can create a user event that is the code you want inside your While Loop. When the Deg Wait Go button is pressed, use the "Generate User Event" node to trigger that event. Do the same thing in the user event case so that the event re-triggers itself if and only if the Abort button has not been pressed.
Option 3. Create a separate loop OUTSIDE your UI loop that does your processing with some sort of command queue running between the UI loop and that other loop. The other loop moves into various states at the request of the UI loop... it's the one that does nothing until it receives a "go" message and then keeps looping until it receives a "stop" message. You can Google "queued message handler" for extensive details of this solution. This is the most common solution for complex UI requirements, especially useful for separating concerns of the UI code from the execution code.

Easy way to pause execution and wait for key press in VBA?

I would like to have a VBA macro pause its execution and wait for the user to press a key before continuing. Is there a quick way to accomplish this, or am I better off using a window popup?
A warning: In my experience, unless your VBA specifically includes DoEvents statements, Office applications are unresponsive while VBA is running.
If you only want to interrupt a process temporarily until the user is ready, then using a pop-up is by far the simplest method.
Adding something like the following would have the desired result:
Msgbox "Click to go on", , "Example"
The user would click to continue or could hit enter or space.
If you wanted a more specific pop-up, loading a modal userform (in Excel) or a modal form (in Access) from code will halt the suspend the calling code until the form is closed.
Honestly, I would resist including a pause unless it's absolutely necessary. Any unnecessary interuption is an annoyance and hinderance to efficiency. If you simply want to inform the user about what is happening you can update the screen to explain what the code is doing as it performs actions.

VB 2010 - Stop a loop with a button

I am using VB 2010 , I have a loop that takes something like 5 minutes until it's finish.
I want to allow the user the option to stop it in the middle or whenever they want.
There's a button which starts the loop , and after I click it the loop starts running and the button is sort of "stuck".
I saw something called "background work" in the VB Toolbox, can it be a solution to my problem?
Start your loop in separate thread and set a flag which directs the action of the loop . Keep polling for this flag to see whether user wants to stop the thread inside the loop on thread . See BackgroundWorker
I think background worker would work, but a very simple solution if to create a boolean variable (visible in scope to your stop button and the logic loop) that controls stopping inside your loop. Your stop button would set the variable to true and the next time that code is hit, it would stop. you may need an application.doevents inside your loop at the end to allow the button's event to fire. This is not an ideal way, but is certainly simple.

Add a spinning wheel while application is searching database

I have an application that searches in a database for some information.
Since the database is quite big, it sometimes takes a lot of time before the application returns the results to the interface.
I want to add some sort of spinning wheel to inform the user that the application is still searching the database and did not freeze. Once the results are returned, the wheel should disappear.
Any idea how to do this or is there a good tutorial explaining how to do this?
Have you considered changing the mouse pointer to the hourglass as this would be extremely simple to implement:
Me.Cursor = Cursors.WaitCursor
...Do your DB calls here...
Me.Cursor = Cursors.Default
However, I would agree that display a 'spinning wheel' is probably a little more user friendly and definately a lot more obvious. So, first get hold of an animated gif that suits your needs. Then create a form that has a picture box containing the image.
Once you have that you can show the form to the user and in the background do the DB work, once this has completed close the form.
Another alternative would be to use a rolling progress bar instead, so when it reaches 100% it cycles around again and keeps going until you close it.
EDIT:
One thing that I forgot to mention is that you will have to handle exception conditions. Lets say you set the cursor to wait, then an error occurs. The exception may bypass the code that resets everything. This leaves the user with a changed cursor and no means of changing it.
When I have done this kind of thing I have typically created a disposable WaitCursor class and then used something like this:
Using myWaitCursor As WaitCursor = New WaitCursor
...do something...
End Using
In the Dispose of the WaitCursor class you set the cursor back to default. The same would apply if you went down the route of using a form with an image or progress bar.
Find an animated gif of such a spinner, like this one. Put it in a PictureBox, set its Visible property to True when you start the job. Beware that you'll have to run the query in a worker thread to keep the animation alive and the user interface responsive. The BackgroundWorker class is good for that.
You could use the Environments Default wait cursor, which for Vista/7 is a circle with the outside spinning, or the XP tumbling hourglass.
You could launch your DB access on a BackgroundWorker and show an animated control such as a Marquee progress bar, or you could show a custom animation to show Busy status.

VB.NET loop through audio

I would like to loop through audio in VB.NET.
Here is my code:
While blnAlert = True
My.Computer.Audio.Play("C:\cat_1.wav")
End While
But it freezes the app.
Cheers.
The code as you've written it will loop without end. The loop conditional is the following
While blnAlert = True
If this is true the loop will be entered. The code inside the loop does nothing to alter this value and hence the condition will always be true. This means the loop will not end.
How do you expect the value of blnAlert to be updated? Can you post some more code or give us a bit more context into the problem?
EDIT OP indicated that the blnAlert will be set to False via button press
The problem is that as the code is written the cancel button cannot be pressed. As soon as the while loop is hit it will not exit. The app will freeze because while your code is executing the form is not able to repaint or handle any user input. You must return control to the application in order to press the cancel button.
Likely the easiest way to solve this problem is to use a timer. Create a timer and during the timer tick function play the sound a single time and leave. Use the cancel button to stop the timer and hence the playing of the sound.
I know the answer given by JaredPar will work and has been accepted, but here's a different (and simpler) solution using the fact that My.Computer.Audio.Play also accepts optional parameters including AudioPlayMode.BackgroundLoop which plays a sound without halting execution until My.Computer.Audio.Stop is called.
So, start playing the sound with:
My.Computer.Audio.Play("mysound.wav", AudioPlayMode.BackgroundLoop)
and in the button-click event:
My.Computer.Audio.Stop()