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

Last updated by William Liebenberg 7 months ago.See history

This rule has been archived
Archived Reason: Deprecated - Silverlight is no longer installable and has been deprecated for 10 years.

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); 
  }));

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();

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

We open source. Powered by GitHub