Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#
Tip/Trick

Using "Run To Cursor" from the Call-stack Window

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
20 Mar 2017CPOL1 min read 8.7K   1   3
Quickly move from one method to another during debugging session

Almost all software developers using Visual Studio are well aware of the most common debugging functions provided in Visual Studio. For example, functionality like “Step Into”, “Step Over” and “Step Out” as shown in figure 1 are few tasks that developers perform quite frequently during their day to day job.

Image 1

However, with the advent of use of Fluent APIs and method chaining techniques (heavily used in frameworks like Linq), some of these tasks can become quite repetitive and cumbersome. Let’s say, you have extended the FileInfo class using Extension methods as follows:

C#
public static class FileInfoExtension
{
    public static IEnumerable<FileInfo>
    IsAcceptableFileType(this IEnumerable<FileInfo> files)
    {
        foreach (FileInfo file in files)
        {
            if (file.Extension == ".jpg" || file.Extension == ".png" )
                yield return file;
        }
    }

    public static IEnumerable<FileInfo>
    IsAcceptableFileSize(this IEnumerable<FileInfo> files)
    {
        foreach (FileInfo file in files)
        {
            if ( file.Length < 1024*2024)
                yield return file;
        }
    }

    public static IEnumerable<FileInfo>
    IsAcceptableFileName(this IEnumerable<FileInfo> files)
    {
        foreach (FileInfo file in files)
        {
            if (file.Name.StartsWith("Figure"))
                yield return file;
        }
    }
}

Nothing as a rocket science in this code, just filtering a List of FileInfo class instances based on file type, file size and file name. Here is an example of how these extension methods are used in the code.

C#
class Program
{
    static void Main(string[] args)
    {
        string path = @"C:\my\CallstackRelatedDemo";
        DirectoryInfo di = new DirectoryInfo(path);
        if (di.Exists)
        {
            var results = di.GetFiles()
                            .ToList()
                            .IsAcceptableFileType()
                            .IsAcceptableFileSize()
                            .IsAcceptableFileName()
                            .Select( x => new
                                {
                                    FileName = x.Name,
                                    Extension = x.Extension,
                                    SizeInKB = x.Length/1024
                                })
                            .ToList();

        }
    }
}

Let’s say during the debugging session, developer steps into the IsAcceptableFileType method. At this point, call-stack will look as follows:

Image 2

If developer wants to continue troubleshooting in IsAcceptableFileName method, she can certainly use “Step Into” and “Step Over” functions but this could be a longer route to get to that method. An easier way is to use “Run To Cursor” approach available within the call-stack window. Right clicking in the call-stack window on the IsAcceptableFileName, a context menu will be displayed as shown in figure 3 below.

Image 3

Choosing “Run To Cursor” item from this context menu will take the developer directly to IsAcceptableFileName method as shown below:

Image 4

This way, you don’t have to click F10/F11/Ctrl+F11 keys multiple times and with just one click, you can start troubleshooting in the method you desire.

Until next, happy debugging.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions

 
Questionlast image is missing Pin
Tridip Bhattacharjee19-Mar-17 22:26
professionalTridip Bhattacharjee19-Mar-17 22:26 
AnswerRe: last image is missing Pin
Kamran Bilgrami20-Mar-17 4:59
Kamran Bilgrami20-Mar-17 4:59 
PraiseNice topic Pin
Nirav Prabtani19-Mar-17 21:55
professionalNirav Prabtani19-Mar-17 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.