Debugging hanging C# program

  • Thread starter Thread starter zfeld
  • Start date Start date
Z

zfeld

I have a multi-threaded C# program that hangs for 5-10 seconds
intermittently. Are there any built in tools in Visual Studio .NET to
somehow instrument my code to see where my performance problems are. It is
rough debugging it using breakpoints due to the multi-threading.
 
To start with I'd give your threads a meaningful name (if at all possible),
then write some sort of debug logger that writes text to disk. From here you
can litter your code with "trace" statements that get logged to a text file.
If you add a time stamp to each entry, you then can trace, from one point in
a thread (using your thread name) to another.

Be sure to make your logging mechanism thread safe.

Here's an example...

#if DEBUG
readonly object grabInterfaceLock = new object();
#endif

public void WriteLog ( string log )
{

#if DEBUG
lock ( grabInterfaceLock )
{
try
{
// filename is...
string filename = @"C:\debuglog.txt";

// prepend the date
log = DateTime.Now.ToString() + " - " + log;

// open up the file and append data
StreamWriter logwriter = File.AppendText(filename);
logwriter.WriteLine( log );
logwriter.Close();
}
catch ( Exception )
{
}
}
#endif

}
 
Back
Top