Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:16:14+00:00 2026-06-14T01:16:14+00:00

I have a function which deserialize an Xml document and create objects from it.

  • 0

I have a function which deserialize an Xml document and create objects from it.

I want the objects to be stored into cache so i don’t have to deserialize the xml every time i need to get data from it.

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

When i want to deserialize the xml, i call

XMLDeserializerFacade.DeserializeXml("file.xml")

My question is that i should use lock also inside the OnElementDeserializedCallback callback as well?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:16:16+00:00Added an answer on June 14, 2026 at 1:16 am

    It depends a lot on the specific implementation. If the code that invokes that is on the same thread as the main lock / DeserializeXml, and it uses the synchronous version (OnElementDeserializedCallback(...) or OnElementDeserializedCallback.Invoke(...)) – then it will already be inside the existing lock, because the lock is essentially tied to the thread.

    If the implementation uses an asynchronous implementation (BeginInvoke, Task, ThreadPool etc), then no: it will not be inside the lock.

    If unsure, you could lock in both places (since locks are re-entrant, it doesn’t matter if you end up having nested locking twice from the same thread); however, if it turns out it is asynchronous, but it also then tries to join back to the callback (Delegate.EndInvoke, Task.Wait, etc) then it could totally deadlock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have JSON response from Facebook, which I don't want to deserialize into a
I have a function which repeatedly indexes into a dictionary with the same key.
XML Serialization from MSDN : Serializes and deserializes objects into and from XML documents.
I have function which selects data from MySQL database: function get_articles( $id ){ global
I have a function which opens a document in Word at a specified location:
I have some objects deserialized from JSON to which I'd like to assign a
I have a function which is called recursively and I want to know the
I have function which performs scatter plot and I want to paste the results(Jpeg
I have function which accepts string (which is basically a XML doc). I am
I have a situation where I have an xml file that I don't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.