Debugging hanging C# program

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.
 
D

Dan Bass

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

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top