我有一个Silverlight 3应用程序,它的效果很好,除了4台机器。 DispatcherTimer和Storyboards在这4台机器上并不一致。我已经创建了一个非常简单的测试应用程序来试图解决这个问题,我将列出下面的代码。Silverlight 1秒DispatcherTimer只在某些机器上每隔30秒发射一次
基本上测试应用程序每秒更新两个TextBlocks。一个使用DispatcherTimer,一个使用Storyboard Animation。这很好 - 文本框每秒更新“1,2,3 ......”。但在4台受影响的机器上,TextBlocks不会每秒更新一次,它们会在27到33秒之间更新。 DispatcherTimer和Storyboard更新在同一时间完成。
CPU,内存,高清都很好。任务管理器和SilverlightSpy显示,这一切都很好。这些都是3 Ghz工作站,其中没有其他运行的3GB RAM。
XAML:
<TextBlock Text="0" Name="DispatcherTimerText" Grid.Column="0" />
<TextBlock Text="0" Name="SBLoopTimerText" Grid.Column="1" />
C#:
Storyboard _sbLoop = new Storyboard();
public MainPage()
{
InitializeComponent();
Storyboard_Start();
Timer_Start();
}
void Timer_Start()
{
System.Windows.Threading.DispatcherTimer dt1 = new System.Windows.Threading.DispatcherTimer();
dt1.Interval = new TimeSpan(0, 0, 1); // 1 second
dt1.Tick += new EventHandler(Timer_Tick);
dt1.Start();
}
void Timer_Tick(object sender, EventArgs e)
{
TextBlock txt = ((TextBlock)LayoutRoot.Children.Single(t => ((TextBlock)t).Name == "DispatcherTimerText"));
txt.Text = (int.Parse(txt.Text) + 1).ToString();
}
void Storyboard_Start()
{
_sbLoop.Duration = TimeSpan.FromSeconds(1);
_sbLoop.Completed += new EventHandler(StoryboardLoop);
_sbLoop.Begin();
}
void StoryboardLoop(object sender, EventArgs e)
{
TextBlock txt = ((TextBlock)LayoutRoot.Children.Single(t => ((TextBlock)t).Name == "SBLoopTimerText"));
txt.Text = (int.Parse(txt.Text) + 1).ToString();
_sbLoop.Begin(); // Restart sb animation
}
更多疑难解答。我在其中一个盒子上安装了Firefox和Chrome,并获得了相同的效果。但每隔一段时间,我就会看到一个5到10秒的运行,它开始正确运行 - TextBlocks每秒更新一次。 5或10秒后,它会回到30秒延迟。 有趣的是,如果你打开所有三个浏览器(IE,FF,Chrome),他们都会一致地做到这一点。也许盒子上某处存在瓶颈。 我不确定从哪里开始排除故障。 IT将禁用MacAfee,我们会看到是否有帮助。 – 2009-11-11 20:18:53