Do you avoid using Thread.Sleep in your Silverlight application?

Updated by Brady Stroud [SSW] 1 year ago. See history

123

Calling Thread.Sleep on your Silverlight application causes the UI thread to sleep. That means the application is not responsive.

If you want to delay something, you can use a storyboard.

Thread.Sleep(5000);
this.Dispatcher.BeginInvoke(new Action(() =>
{
// Try to reconnect in the background
Connect.Execute(null);
}));

❌ Figure: Code: Bad example - Using Thread.Sleep() causes your Silverlight application to freeze

Storyboard sb = new Storyboard() { Duration = TimeSpan.FromSeconds(5) };
sb.Completed += (ds, de) => this.Dispatcher.BeginInvoke(new Action(() =>
{
// Try to reconnect in the background
Connect.Execute(null);
}));
sb.Begin();

✅ Figure: Code: Good example - Use a Storyboard with a duration of the delay and once the Storyboard is finished running

acknowledgements
related rules