none
How to write to single xml file from multiple thread RRS feed

  • Question

  • is it possible to write to single xml file from multiple thread. if yes then please share some sample code example. thanks
    Wednesday, December 25, 2019 8:57 AM

Answers

  • Consider an XmlWriter which is shared between threads. Use lock to ensure thread safety and proper file contents. For example:

    var s = new XmlWriterSettings
    {
           Indent = true
    };
     
    using( XmlWriter w = XmlWriter.Create( "MyFile.xml", s ) )
    {
           w.WriteStartElement( "root" );
     
           var thread1 = new Thread( ( ) =>
                 {
                        for( int i = 0; i < 100; ++i )
                        {
                               lock( w )
                               {
                                      w.WriteStartElement( "Element1" );
                                      w.WriteString( "Value1" );
                                      w.WriteEndElement( );
                               }
                               Thread.Sleep( 10 ); // simulate some delay
                        }
                 } );
           var thread2 = new Thread( ( ) =>
           {
                 for( int i = 0; i < 100; ++i )
                 {
                        lock( w )
                        {
                               w.WriteStartElement( "Element2" );
                               w.WriteAttributeString( "Attr2", "Value2" );
                               w.WriteEndElement( );
                        }
                        Thread.Sleep( 10 ); // simulate some delay
                 }
           } );
     
           thread1.Start( );
           thread2.Start( );
     
           thread1.Join( );
           thread2.Join( );
     
           w.WriteEndElement( );
    }

    See also the members named ‘XmlWriter.xxxAsync’, which are designed for asynchronous operations.




    • Edited by Viorel_MVP Wednesday, December 25, 2019 3:35 PM
    • Marked as answer by Sudip_inn Wednesday, December 25, 2019 5:50 PM
    Wednesday, December 25, 2019 3:31 PM

All replies

  • is it possible to write to single xml file from multiple thread. if yes then please share some sample code example. thanks

    No I don't think you can do this, becuase the O/S is only going to allow one handle on file-open.

    You might be able to open the file, then have a separate function to write to the file that all thread functions could use. You might have to set a flag  like 'writeactive' a Boolean  and make a thread wait until the flag is false to avoid conflict.

    Wednesday, December 25, 2019 1:40 PM
  • Consider an XmlWriter which is shared between threads. Use lock to ensure thread safety and proper file contents. For example:

    var s = new XmlWriterSettings
    {
           Indent = true
    };
     
    using( XmlWriter w = XmlWriter.Create( "MyFile.xml", s ) )
    {
           w.WriteStartElement( "root" );
     
           var thread1 = new Thread( ( ) =>
                 {
                        for( int i = 0; i < 100; ++i )
                        {
                               lock( w )
                               {
                                      w.WriteStartElement( "Element1" );
                                      w.WriteString( "Value1" );
                                      w.WriteEndElement( );
                               }
                               Thread.Sleep( 10 ); // simulate some delay
                        }
                 } );
           var thread2 = new Thread( ( ) =>
           {
                 for( int i = 0; i < 100; ++i )
                 {
                        lock( w )
                        {
                               w.WriteStartElement( "Element2" );
                               w.WriteAttributeString( "Attr2", "Value2" );
                               w.WriteEndElement( );
                        }
                        Thread.Sleep( 10 ); // simulate some delay
                 }
           } );
     
           thread1.Start( );
           thread2.Start( );
     
           thread1.Join( );
           thread2.Join( );
     
           w.WriteEndElement( );
    }

    See also the members named ‘XmlWriter.xxxAsync’, which are designed for asynchronous operations.




    • Edited by Viorel_MVP Wednesday, December 25, 2019 3:35 PM
    • Marked as answer by Sudip_inn Wednesday, December 25, 2019 5:50 PM
    Wednesday, December 25, 2019 3:31 PM