0

I am developing MultiThreaded client-server application in VB.net, where multiple client connect to the one server,there are also process in the server side that open the XML file and write some Data. But after 1 hrs i got the following Error:

"File is used by another proccess."

Is there any suggestion from your side,for above issue. Please check my below code for reading XML

Public Function ISRecordExitsinXML(ByVal pFilepath As String, ByVal pClientIP As String, ByVal pMacAddress As String)
    Dim xmlDoc As XmlDocument = Nothing
    Dim xmlPupilNode As XmlNode = Nothing
    Dim xmlAgeNode As XmlNode = Nothing
    Try

        Dim FileName As String = pFilepath
        ISRecordExitsinXML = Nothing

        xmlDoc = New XmlDocument
        xmlDoc.Load(FileName)
        xmlPupilNode = xmlDoc.SelectSingleNode("//RFIDReader[HostIPAddress = '" & pClientIP.Trim & "' and MacAddress = '" & pMacAddress & "' and Status=1]  ")

        If Not xmlPupilNode Is Nothing Then
            xmlAgeNode = xmlPupilNode.SelectSingleNode("RegDateTime")
            If Not xmlAgeNode Is Nothing Then
                xmlAgeNode.InnerText = DateTime.Now.ToString()
                xmlDoc.Save(FileName)
                ISRecordExitsinXML = True
            End If
        Else
            ISRecordExitsinXML = False
        End If
        xmlDoc = Nothing
        xmlPupilNode = Nothing
        xmlAgeNode = Nothing
    Catch ex As Exception
        xmlDoc = Nothing
        xmlPupilNode = Nothing
        xmlAgeNode = Nothing
        ISRecordExitsinXML = False
        ErrorLog.WriteToErrorLog(ex.Message.ToString(), "HSRV-A9", "ErrorLog.Log")
    Finally
        xmlDoc = Nothing
        xmlPupilNode = Nothing
        xmlAgeNode = Nothing
    End Try
    Return ISRecordExitsinXML
End Function

Thanks in Advance

John
  • 703
  • 7
  • 18
  • 37
  • I would look into thread safety and making certain areas of your application "thread-safe" http://msdn.microsoft.com/en-us/magazine/cc163929.aspx http://msdn.microsoft.com/en-us/library/f857xew0(v=vs.71).aspx – Squirrel5853 Sep 04 '13 at 12:00

2 Answers2

1

Your problem (as I'm sure you are aware!) is that when the file is open by one process, it cannot be opened by another. Is there a possibility of using an alternative medium to receive the data, such as a database? You could then export the data from the database to an XML file in one hit, avoiding the conflict altogether.

Andy
  • 51
  • 4
  • Thanks for Reply,but i have to use XML file only that is the requirement. – John Sep 04 '13 at 12:57
  • OK, no probs. Is it possible to implement a wait feature - if the file is in use then pause for a second or two to allow it time to be written to and close and try again? – Andy Sep 04 '13 at 13:02
0

you may not be closing file after writing process is over, so it will be inaccessible by other threads

Sukhdevsinh Zala
  • 1,126
  • 2
  • 14
  • 19
  • Thanks for replay,I Have a 2 Windows Client Service which running on 2 different Desktops and server is on WAN which also have the Windows Service,so every after 30 sec interval my client services connect to Server and update XML File,till 1 Hour its working fine but after that i getting above Error. – John Sep 04 '13 at 12:54