Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:多线程客户端服务器应用程序_C#_Vb.net_Sockets - Fatal编程技术网

C# 错误:多线程客户端服务器应用程序

C# 错误:多线程客户端服务器应用程序,c#,vb.net,sockets,C#,Vb.net,Sockets,我正在VB.net中开发多线程客户机-服务器应用程序,其中多个客户机连接到一个服务器,服务器端还有打开XML文件和写入数据的进程。 但1小时后,我得到了以下错误: “文件已被另一个进程使用。” 对于上述问题,你们有什么建议吗。 请检查我下面的代码以读取XML Public Function ISRecordExitsinXML(ByVal pFilepath As String, ByVal pClientIP As String, ByVal pMacAddress As String)

我正在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
提前感谢

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

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

我将研究线程安全性,并使应用程序的某些区域“线程安全”,感谢您的重播,我有一个2个Windows客户端服务,它运行在2个不同的桌面上,服务器在WAN上,也有Windows服务,所以每隔30秒我的客户端服务就会连接到服务器并更新XML文件,直到1小时它工作正常,但在那之后我得到了上述错误。感谢回复,但我必须只使用XML文件,这是必要的。好的,没问题。是否可以实现等待功能?如果文件正在使用,请暂停一两秒钟,以便有时间写入并关闭,然后重试?