Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to access The same XML file from Multi Threads That are running from different pages?

I Have Multi threads running each from a different page That want to access an XML file for setting during the thread process.

What should be done to stop this ERROR "The process cannot access the file 'C:\inetpub\wwwroot\XXXXX\App_Data\XML\Setting.xml' because it is being used by another process."


What should be done?

What I have tried:

I have this method used to access the file:

VB
Public Shared SetSettingStsXMLobj As New Object()
   Public Sub SetSettingStsXML()
       SyncLock SetSettingStsXMLobj
           Dim XmlFilename = System.Web.Hosting.HostingEnvironment.MapPath("App_Data\XML\Setting.xml")
           Dim xml As New System.Xml.XmlDocument()
           xml.Load(XmlFilename)
           Dim XXXNodeAs System.Xml.XmlNodeList = xml.SelectNodes("descendant::XXX_Status[X_status ='Active']")
           Dim XXXNode As New List(Of String)
           For Each SerNod As System.Xml.XmlNode In XXXNode
              ......
           Next
           If xxID.Count > 0 Then
               ........
           End If

           Dim activexxDt As DataTable =.......
           activexxDt.TableName = "XXXX_Status"

           activexxDt.WriteXml(XmlFilename, XmlWriteMode.WriteSchema, True)

       End SyncLock

   End Sub



This method located in a Class called by :

VB
 Dim XXXXObj As New XXClass
XXXXObj.SetSettingStsXML()
Posted
Updated 28-Jul-16 4:19am
v2
Comments
F-ES Sitecore 28-Jul-16 10:51am    
I'd probably create a "shared" class that acts as a wrapper for your xml file. Give the shared class a shared constructor and in that constructor read the xml into a private shared xmldocument variable. The class would then have methods like "GetNode" that certain params could be passed into, and those methods will query the xmldocument and return the results.

Using such an implementation will ensure the xml is read only once into the xmldocument and held there, and calling classes access the data through the methods.

To pre-empt your next question, no I won't write it for you, I don't really do vb.net.
Hidhoud1991 29-Jul-16 7:02am    
I did as below but same error: The process cannot access the file 'C:\inetpub\wwwroot\XXXXX\App_Data\XML\Setting.xml' because it is being used by another process.

Public Class XMlFileLock
Private Shared xmlfilename As String
Private Shared xml As New System.Xml.XmlDocument()
Shared Sub New()
xmlfilename = System.Web.Hosting.HostingEnvironment.MapPath("App_Data\XML\Setting.xml")
xml.Load(xmlfilename)
End Sub
Public Shared Sub SetSettingStsXML()
Dim XXXNodeAs System.Xml.XmlNodeList = xml.SelectNodes("descendant::XXX_Status[X_status ='Active']")
Dim XXXNode As New List(Of String)
For Each SerNod As System.Xml.XmlNode In XXXNode
......
Next
If xxID.Count > 0 Then
........
End If

Dim activexxDt As DataTable =.......
activexxDt.TableName = "XXXX_Status"

activexxDt.WriteXml(XmlFilename, XmlWriteMode.WriteSchema, True)
End Sub
End Class

Called by: XMlFileLock.SetSettingStsXML() from every running thread
F-ES Sitecore 29-Jul-16 7:41am    
Mmm, are you sure it is opened somewhere else other than your .net code?
Hidhoud1991 29-Jul-16 7:55am    
its not opened any where else

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900