好的,我有一个棋盘应用程序.该应用程序在一个组框中有64个面板.这些面板用表达式CHessBoardPanels {x,y)标识.我用它来改变面板的背景颜色.我想在chessPanel.BackGround变为红色和面板变回白色之间有一个小的延迟.(大约1到2秒的延迟)我尝试过睡眠功能,但它基本上锁定了应用程序直到任务已经完成完成
这是我尝试过的代码:
for (int Row = 7; Row > 3; --Row)
{
chessBoardPanels[ Column ,Row].BackColor = Color.Red;
++Column;
//Add text to Moves TextBox
MovesText.AppendFormat("WhtB {0} {1}{2}", ColumnText, RowText, Environment.NewLine);
MovesTxt.Text = MovesText.ToString();
++ColumnText;
--RowText;
}
//Start White Horizonal Drill
Column = 0;
Thread.Sleep(5000); //This does not delay proerperly
for (int Row = 7; Row > 4; --Row)
{
chessBoardPanels[Column, Row].BackColor = Color.White;
++Column;
//Add text to Moves TextBox
MovesText.AppendFormat("WhtB {0} {1}{2}", ColumnText, RowText, Environment.NewLine);
MovesTxt.Text = MovesText.ToString();
++ColumnText;
--RowText;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用.Net framework 4.0,因为Visual Studio 2010不支持4.5
这就是睡眠所做的,它会暂停线程.
你需要的是使用一个Timer控件,并创建一个颜色变化的队列,让定时器控件定期调用一个事件处理程序,如下所示:
(在你的Form类中):
Timer timer = new Timer();
Run Code Online (Sandbox Code Playgroud)
(在你的Form的构造函数中):
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 500; // every 1/2 second
timer.Enabled = true;
timer.Start();
Run Code Online (Sandbox Code Playgroud)
然后您的事件处理程序将执行以下操作:
void timer_Tick(object sender, EventArgs e)
{
// Read a queue, that contains timings
var nextItem = PeekAtQueue();
if ((nextItem != null) && (nextItem.WhenToChangeColor <= DateTime.Now))
{
var item = TakeFromQueue(); // as opposed to just peeking
ChangeColor(item);
}
}
Run Code Online (Sandbox Code Playgroud)
这是做什么的?(当然这段代码不是100%完整,你必须自己添加队列访问方法,这取决于你想要/可以使用的内容)
{E5, "red", today at 04:20:30 PM}.| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |