我在程序中使用了richtextbox控件,调用appendtext方法加入文本。我想让每行文本的颜色在红色与黑色之间切换,其实就是实现文字闪烁效果。根据程序要求,我需要把每行文本做成一个线程,然后控制其闪烁,当点击当前文本行时,这行文本停止闪烁,其它文本行照常闪烁。但问题随之而来,我是直接在每个线程中使用了while(true)循环完成闪烁效果,并且还尝试使用了timer完成颜色的切换,由于是跨线程使用控件,所以必须使用委托来实现。但使用委托后所有线程都转到主线程中执行了,造成了线程中的死循环。于是我去掉了委托,并设置了这个属性,System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false,每个线程的文字可以闪烁了,但感觉这样的效果不是很好,而且线程之间不是安全的,怕会有什么问题。如果实现我上述这个文字闪烁的功能还有什么别的好方法吗?或者使用多线程的时候有什么方法去解决上述问题吗?本人第一次做多线程的程序,请多指教?
解决方案 »
- 求Siteserver CMS中所有模板标签的实现代码
- 无法引入命名空间System.Core
- 如何给动态生成的HtmlInputCheckBox旁边添加标签说明
- 递归算法
- 日文系统下的ERP系统
- 作成的widnows 服务如何不可以用任务管理结束
- 求助:Winform中的DataGrid列如何绑定下拉框或者按钮,急~~~~~~~~~~~~~~~~~~~~~~~~
- C#谁能教教我怎样才能用OpenFileDialog打开图片然后插入到RichTextBox中
- 怎么样实现滚屏功能??
- C/S模式下如何让其form2.cs设为启动项
- 调用进程怎么设置优先级?
- serialPort接收数据问题(C#)
{
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);
}
// }
}
}
}
{
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);
}}
例如
while(true)
{
this.Invoke(...)
thread.sleep(1000);
}