0

我正在VB.net中开发多线程客户端-服务器应用程序,其中多个客户端连接到一个服务器,服务器端也有打开XML文件并写入一些数据的进程。但 1 小时后,我收到以下错误:

“文件被另一个进程使用。”

对于上述问题,您有什么建议吗?请检查我下面的代码以阅读 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

提前致谢

4

2 回答 2

1

你的问题(我相信你知道!)是当文件被一个进程打开时,它不能被另一个进程打开。是否有可能使用替代媒介来接收数据,例如数据库?然后,您可以一键将数据从数据库导出到 XML 文件,从而完全避免冲突。

于 2013-09-04T12:00:11.533 回答
0

写入过程结束后您可能不会关闭文件,因此其他线程将无法访问

于 2013-09-04T12:00:29.903 回答