You should never block the UI thread using Sleep
. Instead, you may want to put that part of the code on a background thread. Eg, using Task.Factory.StartNew:
private void Button_Click(object sender, RoutedEventArgs e)
{
Person.FirstName = "John";
Task.Factory.StartNew( () => {
Thread.Sleep(10000);
// ...
});
}
Note that you can't make any other UI changes inside that thread. If you need to, send them back to the UI thread using Dispatcher.BeginInvoke
.
For example, as per the comment:
Person.FirstName = "John";
Task.Factory.StartNew( () => {
foreach (string mlsNumber in ourMlses)
{
Listing listing = sourceSite.ParseByMls(mlsNumber);
if (listing != null)
{
var successDb = sourceSite.UpdateListing(listing);
if (!successDb)
{
throw new Exception("Db error");
}
Dispatcher.BeginInvoke( () => {
// UI update that listing have been added to the DB
});
}
}
} );