C# 线程内的代码不执行,求高手解答
今天碰到一个头痛的问题,我的程序里的线程就不执行
程序到线程开始的循环那里就执行不下去了
而不用线程在 Form1 Load 里就执行得好好的
哪位高手能帮忙看看啊 测试代码如下﹕
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread test1 = new Thread(new ThreadStart(tttt));
test1.IsBackground = true;
test1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
while (true) <---这里的整个循环都没问题
{
string bbb = "123 ";
Thread.Sleep(5000);
}
}
public void tttt()
{
while (true) <---这里就停住了
{
string aaa = "123 ";
Thread.Sleep(5000);
}
}
}
[解决办法]
Thread test1 定义到外面去试试
[解决办法]
public partial class Form1 : Form
{
Thread test1 = new Thread(new ThreadStart(tttt));
public Form1()
{
InitializeComponent();
test1.IsBackground = true;
test1.Start();
}
[解决办法]
非static 的函数里有个this指针
[解决办法]
NO!! 我知道原因了,不知道是不是对的,我觉得Thread.start()不应该在构造函数里写,应该在Form1的Load时间里写,你tttt不要改成 static 你再去试试
public partial class Form1 : Form
{
Thread test1;
public Form1()
{
InitializeComponent();
test1 = new Thread(new ThreadStart(tttt));
}
private void Form1_Load(object sender, EventArgs e)
{
test1.IsBackground = true;
test1.Start();
while (true) <---这里的整个循环都没问题
{
string bbb = "123 ";
Thread.Sleep(5000);
}
}
public void tttt()
{
while (true) <---这里就停住了
{
string aaa = "123 ";
Thread.Sleep(5000);
}
}
}
这样应该没问题了!!
[解决办法]
不要这句试试:
test1.IsBackground = true;
[解决办法]
只要把Thread test1声明在外面就好了,不然的话声明在public Form1()里只是局部变量,等到public Form1()的过程结束以后就不起作用了。
[解决办法]
这样是可以的,你试一下
public partial class Form1 : Form
{
Thread test1;
public Form1()
{
InitializeComponent();
test1 = new Thread(new ThreadStart(tttt));
test1.IsBackground = true;
test1.Name = "test1 ";
test1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
string bbb = "123 ";
Thread.Sleep(1000);
}
}
public void tttt()
{
while (true)
{
string aaa = "123 ";
Thread.Sleep(1000);
}
}
}
[解决办法]
测试通过,没有问题,vs2003
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Thread test1 = new Thread(new ThreadStart(this.tttt));
test1.Start();
}
public void tttt()
{
while (true)
{
MessageBox.Show( "11 ");
Thread.Sleep(1000);
}
}
[解决办法]
走到while (true) 的时候 请按F11 呵呵
[解决办法]
哎呀,没有问题的,你这样试一下吧,定义两个变量,每次循环都自增,可以发现他们都是同步增加的。
public partial class Form1 : Form
{
Thread test1;
int a;
int b;
public Form1()
{
InitializeComponent();
test1 = new Thread(new ThreadStart(tttt));
test1.IsBackground = true;
test1.Name = "test1 ";
test1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
string bbb = "123 ";
Thread.Sleep(1000);
a++;
}
}
public void tttt()
{
while (true)
{
string aaa = "123 ";
Thread.Sleep(1000);
b++;
}
}
}
[解决办法]
多线程的例子
http://blog.csdn.net/hertcloud/archive/2007/04/07/1556112.aspx
[解决办法]
可能是因为你在调试的原因,导致效果没有显示
你试一下用调试输出,不要断点
public partial class Form1 : Form
{
Thread test1;
int a;
int b;
public Form1()
{
InitializeComponent();
test1 = new Thread(new ThreadStart(tttt));
test1.IsBackground = true;
test1.Name = "test1 ";
test1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
string bbb = "123 ";
Thread.Sleep(1000);
a++;
Debug.WriteLine( "a= "+a.ToString());
}
}
public void tttt()
{
while (true)
{
string aaa = "123 ";
Thread.Sleep(1000);
b++;
Debug.WriteLine( "b= "+b.ToString());
}
}
}