我想有一種方法可以在開始另一個呼叫之前等待5秒鐘內完成一個方法。這就像它首先顯示一個「你好」,然後等待5秒鐘,然後顯示「世界」,並等待5秒鐘再次顯示這兩個消息。我創建了一個DispatcherTimer方法,但它在等待5秒鐘內快速顯示兩個文本。在啓動另一個方法之前等待一個固定的時間
private void AutoAnimationTrigger(Action action, TimeSpan delay)
{
timer2 = new DispatcherTimer();
timer2.Interval = delay;
timer2.Tag = action;
timer2.Tick += timer2_Tick;
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2 = (DispatcherTimer)sender;
Action action = (Action)timer2.Tag;
action.Invoke();
timer2.Stop();
}
if (counter == 0)
{
AutoAnimationTrigger(new Action(delegate { MessageBox.Show("Hello"); }), TimeSpan.FromMilliseconds(5000));
AutoAnimationTrigger(new Action(delegate { MessageBox.Show("World"); }), TimeSpan.FromMilliseconds(5000));
}
我該錯過什麼或做錯了什麼?
edit___
ThreadPool.QueueUserWorkItem(delegate
{
//Thread.Sleep(5000);
Dispatcher.Invoke(new Action(() =>
{
TranslateX(4);
TranslateY(-0.5);
}), DispatcherPriority.Normal);
//Dispatcher.BeginInvoke(new Action(() =>
//{
// TranslateY(0.5);
//}), DispatcherPriority.Normal);
});
然後我就簡單地調用方法..
你能準確地知道你使用的是什麼C#版本嗎?在顯示消息時,應用程序的其餘部分是否仍在運行? – Ucodia
c#4.0 ..我很抱歉.. messagebox只是一種方式來顯示我的代碼正在運行,僅用於測試,實際上我想在kinect設備未檢測到任何骨架時爲模型創建動畫。 – user1884304