我在程序中使用了richtextbox控件,调用appendtext方法加入文本。我想让每行文本的颜色在红色与黑色之间切换,其实就是实现文字闪烁效果。根据程序要求,我需要把每行文本做成一个线程,然后控制其闪烁,当点击当前文本行时,这行文本停止闪烁,其它文本行照常闪烁。但问题随之而来,我是直接在每个线程中使用了while(true)循环完成闪烁效果,并且还尝试使用了timer完成颜色的切换,由于是跨线程使用控件,所以必须使用委托来实现。但使用委托后所有线程都转到主线程中执行了,造成了线程中的死循环。于是我去掉了委托,并设置了这个属性,System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false,每个线程的文字可以闪烁了,但感觉这样的效果不是很好,而且线程之间不是安全的,怕会有什么问题。如果实现我上述这个文字闪烁的功能还有什么别的好方法吗?或者使用多线程的时候有什么方法去解决上述问题吗?本人第一次做多线程的程序,请多指教?

解决方案 »

  1.   

    1楼,就是在执行第一个线程的时候就进行那while(true)循环了。
      

  2.   

    public class TextFlash
        {
            private Thread FlashThread;
            private RichTextBox oText;
            //private TextBox oText;
            private System.Timers.Timer TColor;
            TextFormat tf = new TextFormat();        public TextFlash(RichTextBox tb,int startpos,int textlength)
            {
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                
                oText = tb;
                tf.StartPos = startpos;
                tf.TextLength = textlength;
                //oText.Select(startpos, textlength);
                TColor = new System.Timers.Timer(1000);
                TColor.Elapsed += new System.Timers.ElapsedEventHandler(TColor_Elapsed);
            }        private void TColor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                lock (oText)
                {
                    if (oText.SelectionColor == Color.Red)
                    {
                        oText.Select(tf.StartPos, tf.TextLength);
                        oText.SelectionColor = Color.Black;
                    }
                    else
                    {
                        oText.Select(tf.StartPos, tf.TextLength);
                        oText.SelectionColor = Color.Red;
                    }
                }
            }        //~ TextFlash()
            //{
            //    FlashThread.Abort();
            //}
            public void Dispose()
            {
                //FlashThread.Abort();
                tf = null;
                TColor.Enabled = false;
            }        public void StartThread()
            {
                FlashThread = new Thread(new ParameterizedThreadStart(FlashText));
                FlashThread.Start(tf);
                //TColor.Enabled = true;
            }        delegate void FlashTextHandler(object state);
            private void FlashText(object state)
            {
                //if (oText.InvokeRequired)
                //{
                //    FlashTextHandler dd = new FlashTextHandler(FlashText);
                //    oText.BeginInvoke(dd,state);
                //}
                //else
                //{
                    while (true)
                    {
                        lock (oText)
                        {
                            oText.Select(((TextFormat)state).StartPos, ((TextFormat)state).TextLength);
                            oText.SelectionColor = Color.Red;
                            Thread.Sleep(1000);
                            oText.Select(((TextFormat)state).StartPos, ((TextFormat)state).TextLength);
                            oText.SelectionColor = Color.Black;
                            Thread.Sleep(1000);
                            //oText.ForeColor = Color.Red;
                            //Thread.Sleep(500);
                            //oText.ForeColor = Color.Black;
                            //Thread.Sleep(500);
                        }
                //    }
                    }
            }
        }
      

  3.   

    void ThreadMethod()
    {
    while(true)
    {
          FlashTextHandler dd = new FlashTextHandler(FlashText);
          oText.BeginInvoke(dd,state);
          Thread.Sleep(1000);
    }
    }
    void FlashText(object obj)
    {
        lock (oText)
      {
      oText.Select(((TextFormat)state).StartPos, ((TextFormat)state).TextLength);
      oText.SelectionColor = Color.Red;
      Thread.Sleep(1000);
      oText.Select(((TextFormat)state).StartPos, ((TextFormat)state).TextLength);
      oText.SelectionColor = Color.Black;
      Thread.Sleep(1000);
      //oText.ForeColor = Color.Red;
      //Thread.Sleep(500);
      //oText.ForeColor = Color.Black;
      //Thread.Sleep(500);
      }}
      

  4.   

    麻烦LZ加我QQ:260051219,我最近也做了个这么个东西,要闪烁的,第一次是用线程实现的,第二次是用timer实现的,希望可以交流一下!
      

  5.   

    while中使用到界面的时候才用invoke,不要对整个while用invoke
    例如
    while(true)
    {
        this.Invoke(...)
        thread.sleep(1000);
    }