// Declare a private variable to store application state.
private WeatherData _remoteWeatherData;
+ private readonly Object _lock = new Object();
// Declare a public property to access the application data variable.
public WeatherData ApplicationDataObject
{
- get { return _remoteWeatherData; }
+ get
+ {
+ lock (_lock)
+ {
+ return _remoteWeatherData;
+ }
+ }
set
{
- if (value != _remoteWeatherData)
+ lock (_lock)
{
- _remoteWeatherData = value;
+ if (value != _remoteWeatherData)
+ {
+ _remoteWeatherData = value;
+ }
}
}
}
// If the application member variable is not empty,
// set the page's data object from the application member variable.
- // TODO: I am setting and getting ApplicationDataObject from different threads!!!! What if I do not see its last value? Do I need synchronization? :/
WeatherData weatherData = (Application.Current as WeatherInformation.App).ApplicationDataObject;
if (!IsDataFresh(locationItem.LastRemoteDataUpdate) || weatherData == null)
{
// Load remote data (aynchronous way by means of async/await)
// Gets the data from the web.
- // TODO: multiple threads writing/reading same data :(
(Application.Current as WeatherInformation.App).ApplicationDataObject =
await GetRemoteDataAsync(locationItem).ConfigureAwait(false);