我想在TextBlock中短时间显示一条消息。 我正在使用此代码
Label1.Text = "Wrong Password!";
System.Threading.Thread.Sleep(5000);
Label1.Text = " ";
但这不起作用,如果有人有其他更好的逻辑,那么请回答!
答案 0 :(得分:1)
上面的代码将睡眠UI线程,所以基本上发生的是:
要解决此问题,请执行以下操作:
Label1.Text = "Wrong Password!";
// start a new background thread
new Thread(new ThreadStart(() =>
{
Thread.Sleep(5000);
// interacting with Control properties must be done on the UI thread
// use the Dispatcher to queue some code up to be run on the UI thread
Dispatcher.BeginInvoke(() =>
{
Label1.Text = " ";
});
})).Start();
这将: