当前位置:编程学习 > C#/ASP.NET >>

求解C#一个线程问题

一个线程的程序刚开始跑的时候是可以的,但跑了一次现在又不行了,出现这样的问题,该怎么改呀?
控制灯来回变颜色的
private void startChange()
        {
            Thread threadChange = new Thread(new ThreadStart(changeLight));
            threadChange.Start();

        }

        private void changeLight()
        {
            bool light = true;
            while (light)
            {
                if (zuol1.BackColor == Color.White)
                {
                    zuol1.BackColor = Color.Green;
                    Thread.Sleep(100);
                }
                else if (zuol1.BackColor == Color.Green)
                {
                    zuol1.BackColor = Color.Red;
                    Thread.Sleep(100);
                }
                else
                {
                    zuol1.BackColor = Color.White;
                    Thread.Sleep(100);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            startChange();
        }

--------------------编程问答-------------------- 需要用委托,

this.label_AcciTime.Invoke(
                                new MethodInvoker(
                                delegate
                                {
                                    label_AcciTime.Text = ""; ;//事件发生时间
                                }));


label_AcciTime 换成你自己的控件即可。 --------------------编程问答-------------------- this.Invoke(new Action(() =>
                        {
                            //这里放置操作界面UI的方法
                        }));
--------------------编程问答-------------------- 两种方法,第一种简单但建议使用第二种

CheckForIllegalCrossThreadCalls = false;



Invoke((MethodInvoker)(() => {//更改颜色的代码 }));
--------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 3 楼 bigbaldy 的回复:
两种方法,第一种简单但建议使用第二种

CheckForIllegalCrossThreadCalls = false;



Invoke((MethodInvoker)(() => {//更改颜色的代码 }));

你说的第一个方法是可以的,但第二个就不能了啊~这怎么回事呀
Invoke((MethodInvoker)(() => 
            {
                while (light)
                {
                    if (zuol1.BackColor == Color.White)
                    {
                        zuol1.BackColor = Color.Green;
                        Thread.Sleep(100);
                    }
                    else if (zuol1.BackColor == Color.Green)
                    {
                        zuol1.BackColor = Color.Red;
                        Thread.Sleep(100);
                    }
                    else
                    {
                        zuol1.BackColor = Color.White;
                        Thread.Sleep(100);
                    }
                }
            }));        
--------------------编程问答--------------------
                        this.Invoke(new MethodInvoker(() =>
                        {
                            while (light)
                            {
                                if (zuol1.BackColor == Color.White)
                                {
                                    zuol1.BackColor = Color.Green;
                                    Thread.Sleep(100);
                                }
                                else if (zuol1.BackColor == Color.Green)
                                {
                                    zuol1.BackColor = Color.Red;
                                    Thread.Sleep(100);
                                }
                                else
                                {
                                    zuol1.BackColor = Color.White;
                                    Thread.Sleep(100);
                                }
                            }
                        }));
--------------------编程问答-------------------- .net 跨线程访问到UI的控件时,不能直接访问,详情看这个面的
http://zhidao.baidu.com/link?url=DJZgHHDxZ4YBRj5koZMlXVWv2A4cxVGf-zj_S2d-wQ5f300QG2sEAJyBspzPDvGczBqX0qQ9rlnoDyxARbRPUK --------------------编程问答-------------------- 线程里面不能直接使用控件,需要使用委托。 --------------------编程问答-------------------- 楼上N个正确答案了 --------------------编程问答--------------------
引用 9 楼 hunter103 的回复:
楼上N个正确答案了

楼上的答案我都试过了,但都要把while语句去掉才可以。是不是我这样做本身就是个错误 --------------------编程问答--------------------

        private void changeLight()
        {
            bool light = true;
            while (light)
            {
                this.Invoke(new MethodInvoker(() =>
                {
                if (zuol1.BackColor == Color.White)
                {
                    zuol1.BackColor = Color.Green;
                    Thread.Sleep(100);
                }
                else if (zuol1.BackColor == Color.Green)
                {
                    zuol1.BackColor = Color.Red;
                    Thread.Sleep(100);
                }
                else
                {
                    zuol1.BackColor = Color.White;
                    Thread.Sleep(100);
                }                    
                }));
                
            }
        }
--------------------编程问答--------------------

        private void changeLight()
        {
            bool light = true;
            while (light)
            {
               
                Color newColor=Color.Green;
                if (zuol1.BackColor == Color.White)
                {
                   newColor = Color.Green;                   
                }
                else if (zuol1.BackColor == Color.Green)
                {
                    newColor= Color.Red;
                }
                else
                {
                   newColor= Color.White;                    
                }  
                this.Invoke(Action(() =>
                {             
                   zuol1.BackColor=newColor;//只在需要操作界面的时候才用invoke/begininvoke
                   Thread.Sleep(300);//时间过短,有可能导致时间占用太多。因为Invoke本质上是由主线程的执行的
                }));
                
            }
        }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,