日期:2014-05-19  浏览次数:20905 次

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()