【发布时间】:2013-07-19 23:29:54
【问题描述】:
我希望有一种方法可以在开始另一个调用之前等待一个方法在 5 秒内完成。就像它首先显示“Hello”然后等待 5 秒,然后显示“World”并再等待 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));
}
我错过了什么或做错了什么?
编辑___
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# 版本吗?当您显示消息时,应用程序的其余部分是否应该继续运行?
-
c# 4.0.. 对不起.. 消息框只是我显示我的代码正在运行的一种方式,只是为了测试,实际上我想在 kinect 设备未检测到时为模型设置动画任何骨架..
标签: c# wpf threadpool dispatcher