I use this code pattern all over and only in this particular property an ArgumentNullException is thrown when trying to assign a value to it (ie invoking the setter). The object being null is the isLoggedInLock,so it seems that it has not been instantiated before the setter is called. Wat is wrong??
EDIT: I commented out the OnPropertyChanged method to avoid confusion. The exception is still thrown. One other thing I can think of is that the update is done in another thread than the UI thread.
EDIT2: I instantiated the isLoggedInLock in the non-static constructor and still the exception is thrown. Something very strange is going on. I will investigate further.
FINAL EDIT: My apologies: I left out the key to the solution: the [DataMember]
Attribute. The object comes into life by being deserialized. This skips the default constructor and the static initialisation of the isLoggedInLock. By adorning the isLoggedInLock with the [DataMember]
attribute or by instantiating it in a method adorned with the [OnDeserialzed]
attribute the problem goes away!
Thanks a bunch and the first to update his answer will get the points! ;-)
[DataMember]
private bool isLoggedIn;
private readonly object isLoggedInLock=new object();
public bool IsLoggedIn
{
get
{
lock (isLoggedInLock)
{
return isLoggedIn;
}
}
set
{
lock (isLoggedInLock)
{
isLoggedIn = value;
//OnPropertyChanged("IsLoggedIn");
}
}
}