Debugging multi-threaded operations problem
I've ran into a rather odd problem, both in the application I'm in the process of developing and in a test project I threw together just to replicate the issue.
When debugging code that is run on a thread other than the main execution thread the non-main thread will exit for no apparent reason. Here's the kicker... It only happens if you have the autos variable window selected i.e. the problem will not show if you're viewing the watch list.
Here's how to replicate:
Visual Studio 2005 Standard 8.0.50727.42
1 Create a new Windows Application for C#.
2 Create 1 button.
3 Use the following for your class code.
public partial class Form1 : Form
{
Thread thread = null;
ThreadStart ts = null;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
ts = new ThreadStart(myfunc);
thread = new Thread(ts);
thread.Name = "MyThread";
thread.Start();
}
protected void myfunc()
{
int jj = 0;
for (int i = 0; i < 100; i++)
{
jj += i;
Thread.Sleep(1000);
}
}
}
4 Set a breakpoint on the jj += i; line
5 Start debugging the application
6 Click the button1
7 Wait for a long time while System.Configuration dll to load...
8 Make sure you have the Autos window open
8 Hit F10 to step over
9 Watch your thread exit in the output window...
10 Stop debugging
11 Restart debugging
12 Click the button1
13 etc
14 Make sure you are not viewing the Autos window (switch to Watch 1 or something)
15 Hit F10 to step over
16 Ponder why in the world this made any difference.
17 Submit problem to forum
18 Type this message...
Anyone have any ideas on this?
Thanks,
Scott

