4
我有一個反序列化Xml文檔並從中創建對象的函數。C#:lock語句中的回調函數是否也被鎖定?
我想將對象存儲到緩存中,所以我不需要每次需要從中獲取數據時反序列化xml。
public class XMLDeserializer
{
public event OnElementDeserialized OnElementDeserializedCallback;
public void DeserializeXml(string xmlPath)
{
// implementation
}
}
public class XMLDeserializerFacade
{
private static object _lockObject = new object();
private XMLDeserializer xmlDeserializer;
private ICacheProvider cacheProvider;
public XMLDeserializerFacade(XMLDeserializer xmlDeserializer, ICacheProvider cacheProvider)
{
this.xmlDeserializer = xmlDeserializer;
this.cacheProvider = cacheProvider;
xmlDeserializer.OnElementDeserializedCallback += delegate(object element)
{
cacheProvider.Add("uniqueKey", element);
// is here in lock as well or i have to lock it again?
};
}
public void DeserializeXml(string xmlPath)
{
lock(_lockObject)
{
xmlDeserializer.DeserializeXml(xmlPath);
// From here it will go to
// cacheProvider.Add("uniqueKey", element); callback
}
}
}
當我要反序列化的XML,我叫
XMLDeserializerFacade.DeserializeXml("file.xml")
我的問題是,我應該使用lock
也OnElementDeserializedCallback
回調裏面呢?
謝謝
我會雙鎖只是爲了確保它鎖定:) – Catalin
完全死鎖:) – flindeberg