How to find where my code gets into an infinite loop

rael isaac 41 Reputation points
2024-07-13T13:13:22.4633333+00:00

My Visual Basic code is getting stuck somewhere and never finishes. This only happens with one particular input, otherwise it works. I'd like to know at what routine the code is getting stuck. I looked for advice on the web, and it says that when the program seems to be in an infinite loop, I should click the 'pause' button. The problem with that advice is that I don't find a pause button. I tried clicking on 'debug / stop debugging', but I don't think that told me where the code got stuck.

Is there a way to pinpoint the line or lines causing the problem?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,879 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,144 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,729 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 31,801 Reputation points Microsoft Vendor
    2024-07-15T06:18:41.85+00:00

    Hi @rael isaac ,

    Check the following steps about debugging.

    Using Breakpoints

    Set Breakpoints:

    • Open your Visual Basic project in Visual Studio.
    • Navigate to the code file where you suspect the issue might be occurring.
    • Click in the left margin next to the line numbers to set a breakpoint. A red dot will appear indicating a breakpoint.
    • Set breakpoints at the beginning of your main routines or suspected problem areas.

    Run Your Program:

    • Start debugging by pressing F5 or selecting Debug > Start Debugging from the menu.
    • Your program will run and pause execution at each breakpoint you have set.

    Step Through the Code:

    • Use F10 (Step Over) to execute the next line of code without stepping into functions.
    • Use F11 (Step Into) to step into functions to see what happens inside them.
    • Use Shift + F11 (Step Out) to step out of the current function.

    Using the Pause Button

    Start Debugging:

    • Start debugging your program by pressing F5.

    Pause Execution:

    • While the program is running and appears to be stuck, go to the Debug menu and select Break All (or press Ctrl + Alt + Break).
    • This will pause the execution of all threads in your program.

    Examine the Code:

    • Visual Studio will show you where the code is currently paused.
    • You can check the call stack, local variables, and other debugging windows to understand the current state of the program.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.