VS2005下如何调试多线程的C#程序?

lz_0618 2008-06-20 11:03:57
我在线程执行的方法(如foo)设置断点,调试器是停下来了,但F11单步执行不行(和F5效果一样,是连续执行)?
在主线程中(如断点一处)单步执行没有问题


private void menuItem2_Click(object sender, System.EventArgs e)
{
Thread thread=new Thread(new ThreadStart(this.foo));
thread.Start(); //在这一行设置第一个断点
}

public void foo()
{
this.textBox1.Text+="blah..blah..";  //在这一行设置第二个断点
.......
}
...全文
999 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
watertung 2008-10-27
  • 打赏
  • 举报
回复
up
winner2050 2008-06-21
  • 打赏
  • 举报
回复
多线程里面不能直接对非自己创建的控件进行赋值.

虽然运行起来不错报错,但是调试的时候会报错.

#region 跨线程控制控件
public delegate void OutPutDelegate(string outputStr);
public void OutPut(string outputStr)
{
if (this.InvokeRequired)
{
OutPutDelegate opd = new OutPutDelegate(OutPut);
this.BeginInvoke(opd, new object[] { outputStr });
}
else
{
textBox1.Text = outputStr;
}
}
#endregion


private void menuItem2_Click(object sender, System.EventArgs e)
{
Thread thread=new Thread(new ThreadStart(this.foo));
thread.Start(); //在这一行设置第一个断点
}

public void foo()
{

OutPut( textBox1.Text + "blah..blah..");
}
lz_0618 2008-06-21
  • 打赏
  • 举报
回复
看来VS2005在多线程调试方面确实有问题,不知vs2008怎么样
yagebu1983 2008-06-21
  • 打赏
  • 举报
回复
你不会加几个输出啊!!!
lz_0618 2008-06-21
  • 打赏
  • 举报
回复
下面是一个简单的多线程的例子,在Form 上放一Button,Button的click事件中启动线程,调试时在 int iTmp = 50;语句处设置断点,运行程序时,在断点处能停下,但按F11或F10单步执行时,不行,实际上连续执行了!!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadDebug
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(ThreadFun);
th.Start();

}
private void ThreadFun()
{
int iTmp = 50;
for (int i = 0; i < 10000; i++)
{
iTmp += i;
}
}
}

}
lz_0618 2008-06-21
  • 打赏
  • 举报
回复
代码太多了,贴不出来,有时间做个简单的,应该也是一样,单步走不了
fuadam 2008-06-20
  • 打赏
  • 举报
回复
foo具体贴下代码、、、、

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧