CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    Simultaneous Serializing and Deserializing

    Hello,

    I have an application "X" which is responsible for:
    - Collecting a data from lab
    - Open or create an xml file
    - Fill the xml file with the data collected

    Simultaneously, another application say "Y" is reading this xml and updating the data retrieved in a GUI. Application Y is only reading the xml while X has read and write access. So far so good but since the data is collected over a couple of days and is asynchronous and meanwhile in huge blocks, I am facing difficulty in serializing.

    Code:
    RecieveData(double aX, double aY, double aZ) //In application X
    {
                _fStream = new FileStream(mFileName, FileMode.OpenOrCreate);
                _xmlSerialize = new XmlSerializer(typeof(Data));
                _3dData.Coordinates.Add(aX, aY, aZ);
                _xmlSerialize.Serialize(_fStream, _3dData);    //_3dData.Coordinates is a list
                _fStream.Close();
    }
    
    PlotData(string aTargetFile) //In application Y
    {
                _sourceFileXml = aTargetFile;
                FileStream fs = new FileStream(_sourceFileXml, FileMode.Open, FileAccess.Read);
                XmlSerializer xs = new XmlSerializer(typeof(Data));
                _plot = (Data)xs.Deserialize(fs);
                fs.Close();
    }
    Problem:
    ----What you can see in above code is that I receive a single set of coordinate at a time. I add it to a list and then I serialize the whole data again and then I close the _fStream.
    ----The above method I find inefficient as I am opening and closing stream and serializing from the beginning _3dData.Coordinate.

    Question:
    -----Is there any method to keep the stream open and just append the new coordinates to the xml file and at the same time keep on reading the xml file from application Y without opening and closing stream there too?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simultaneous Serializing and Deserializing

    You need to provide locking so that the file can only be accessed by the receive or plot method at a time (but not by both at once).

    You can provide the synchronization by using a named mutex that gets shared between processes, then change your code to something like:

    Code:
    ReceiveData(double aX, double aY, double aZ) //In application X
    {
       try
       {
           // Lock the shared mutex
    
                _fStream = new FileStream(mFileName, FileMode.OpenOrCreate);
                _xmlSerialize = new XmlSerializer(typeof(Data));
                _3dData.Coordinates.Add(aX, aY, aZ);
                _xmlSerialize.Serialize(_fStream, _3dData);    //_3dData.Coordinates is a list
                _fStream.Close();
    
        }
       finally
       {
          // Unlock the shared mutex
       }
    }
    
    PlotData(string aTargetFile) //In application Y
    {
       try
       {
           // Lock the shared mutex
    
                _sourceFileXml = aTargetFile;
                FileStream fs = new FileStream(_sourceFileXml, FileMode.Open, FileAccess.Read);
                XmlSerializer xs = new XmlSerializer(typeof(Data));
                _plot = (Data)xs.Deserialize(fs);
                fs.Close();
    
        }
       finally
       {
          // Unlock the shared mutex
       }
    }
    As an aside, prefer to wrap the file stream objects in a using block (e.g. using(var fs = FileStream(...)) rather than explicitly calling fs.Close().

    This is preferable because in the code above (your code or mine), if an exception is thrown between the creation of the fs object and before fs.Close() is called, the file object won't get released. If you put the file stream object inside a using block, it will always get release whether or not an exception gets thrown.

  3. #3
    Join Date
    Mar 2015
    Posts
    4

    Re: Simultaneous Serializing and Deserializing

    For your goals, I wouldnt use this architecture.. I would follow the old EDI method of file processing..
    Instead of having one file that is read from and written two, I would rather have my X application create a new file everytime it have something to write, and have my Y application read and delete the file once it is done.

    Also, why not use JSON, its simpler and faster!

    Check out this example on DotNetPad: http://dotnetpad.com/4237wsgc

    Hope that helps,

    Let me know if you need anything else

    Thanks
    Firas

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simultaneous Serializing and Deserializing

    Is there any method to keep the stream open and just append the new coordinates to the xml file and at the same time keep on reading the xml file from application Y without opening and closing stream there too?
    The simple answer is NO.... This is because in order to Read a valid XML file it must have the closing tag. However to APPEND, the closing tag must not yet have been written.

    I would give SERIOUS thought to the overall architecture. Before I could make any recommendations, there would be a need for more details.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Aug 2008
    Posts
    78

    Resolved Re: Simultaneous Serializing and Deserializing

    I did it with .txt file. It works fine.
    One application is writing to the file and another application is just reading from it whenever the file is changed.

    Thanks

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simultaneous Serializing and Deserializing

    Quote Originally Posted by ricky_cpp View Post
    I did it with .txt file. It works fine.
    One application is writing to the file and another application is just reading from it whenever the file is changed.
    Thanks
    As an XML File???? Or as some type of "log"/cvs/etc. Linear file?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simultaneous Serializing and Deserializing

    Quote Originally Posted by ricky_cpp View Post
    I did it with .txt file. It works fine.
    One application is writing to the file and another application is just reading from it whenever the file is changed.

    Thanks
    If you don't synchronize access to the file and it works, it's because you are getting lucky.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simultaneous Serializing and Deserializing

    Quote Originally Posted by Arjay View Post
    If you don't synchronize access to the file and it works, it's because you are getting lucky.
    Synchronization is critical (And can be done in many ways)....but that does not address:
    -----Is there any method to keep the stream open and just append the new coordinates to the xml file
    A valid XML file *must* have matching tags...so the last content at any point (where the file can be considered a valid xml) file must be the closing tag which matches the first opening tag.

    Additional content must be (effectively) inserted prior to this tag...which violates the "just append"...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Simultaneous Serializing and Deserializing

    Quote Originally Posted by TheCPUWizard View Post
    Synchronization is critical (And can be done in many ways)....but that does not address:


    A valid XML file *must* have matching tags...so the last content at any point (where the file can be considered a valid xml) file must be the closing tag which matches the first opening tag.

    Additional content must be (effectively) inserted prior to this tag...which violates the "just append"...
    Right, I wasn't attempting to solve all the world's problems - just addressing the non-synchronized file case (where the op mentioned the .txt file).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured