今天碰到一个头痛的问题,我的程序里的线程就不执行
程序到线程开始的循环那里就执行不下去了
而不用线程在 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);
            }
        }
    }

解决方案 »

  1.   

    Thread test1 定义到外面去试试
      

  2.   

    to:showjancn(难得一剑)试过了 没用
      

  3.   

    public partial class Form1 : Form
        {
            Thread test1 = new Thread(new ThreadStart(tttt));
            public Form1()
            {
                InitializeComponent();
                test1.IsBackground = true;
                test1.Start();
            }
      

  4.   

    to: yf370768770
    放在这里再把 tttt()方法改成 static 真的就可以了能请问一下原因吗﹖
    我还是不太理解.呵呵
      

  5.   

    又试了一下 发现在 Thread test1 定义在里面 外面都一样
    只要把 tttt 方法改成静态就可以了
      

  6.   

    非static 的函数里有个this指针
      

  7.   

    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);
                }
            }
    }
    这样应该没问题了!!
      

  8.   

    不要这句试试:
    test1.IsBackground = true;
      

  9.   

    To : yf370768770
    这样还是不行  我试过了
    目前只知道改静态方法这一种办法TO : tdtdtdtdtd123(飘)
    大侠能详细说明下原理吗﹖
      

  10.   

    只要把Thread test1声明在外面就好了,不然的话声明在public Form1()里只是局部变量,等到public Form1()的过程结束以后就不起作用了。
      

  11.   

    这样是可以的,你试一下    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);
                }
            }
        }
      

  12.   

    TO: mrcooldog你的程序试过了 还是不行
    你的程序在你的计算机上能运行到 tttt() 的while 循环吗﹖
    不会是我的计算机的问题吧
      

  13.   

    在我的电脑上tttt() 线程 只能运行到while(true) 就进行不下去了
      

  14.   

    可以啊,你把里面加上断点,可以进去的,应该不是机器的问题。把tttt()里的
                while (true)                string aaa = "123";
                    Thread.Sleep(1000);
    三行都加上断点把Form1_Load()里的
                while (true)
                    string bbb = "123";
                    Thread.Sleep(1000);
    三行也都加上断点不要用单步执行,那样有可能看不出来。
      

  15.   

    测试通过,没有问题,vs2003public 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);
     }
    }
      

  16.   

    走到while (true) 的时候 请按F11 呵呵
      

  17.   

    哎呀,没有问题的,你这样试一下吧,定义两个变量,每次循环都自增,可以发现他们都是同步增加的。
        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++;
                }
            }
        }
      

  18.   

    多线程的例子http://blog.csdn.net/hertcloud/archive/2007/04/07/1556112.aspx
      

  19.   

    可能是因为你在调试的原因,导致效果没有显示
    你试一下用调试输出,不要断点    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());
                }
            }
        }