Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Using Run To Cursor from the Call-stack Window

5.00/5 (5 votes)
20 Mar 2017CPOL1 min read 9.4K  
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)