如何摆脱访问同一文件的两种方法之间的等待时间?

我有以下VBSCript代码片段在纯文本文件中查找部分(strSection)和该部分中的项目(strItem)。
Do While Not myINIFile.AtEndOfStream
  sl_Sleep(100)
  s = myINIFile.ReadLine <--- **This is the line that rises the exception**
  If s = strSection Then
    'Find the correct line
    Do While Not myINIFile.AtEndOfStream
      s = myINIFile.ReadLine
      If InStr(s, strItem)>0 Then
        Exit Do
      End if              
    Loop            
    Exit Do
  End if              
Loop 
似乎如果我没有放入Sleep(100)(毫秒)(或使用较低的值,如20或50),我最终得到一个异常,说“进程无法访问该文件,因为另一个进程已锁定文件的一部分”。 我认为Do While中的条件是在AtEndOfStream尚未完成时调用ReadLine方法。但是,每行使用100毫秒会使进程速度降低到不可接受的速度。 这是真正发生的事情还是其他地方的问题? 是否有任何我可以做的事情来防止这个进程被锁定或等待一段时间(直到它被释放)只有它被锁定所以它不会引发异常? 编辑:此代码循环通过如下所示的文件:
[Section1]    
param1 = 100    
param2 = 200    
param3 = 300    

[Section2]    
param1 = 1000    
param2 = 2000    
param3 = 3000

[Section3]    
param1 = 1
param2 = 2
param3 = 5
param4 = 10
param5 = 20
代码应该通过文件,直到找到[SectionX]行(strSection),然后继续读取行,直到找到“paramx”行(strItem)。然后将该行保存在's'中,稍后处理。     
已邀请:
你发布的代码对我来说非常合适。我没有例外...... 你在使用你发布的代码时在其他任何地方访问该文件? 你试过读取整个文件,然后解析字符串?见http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/     
在这里,这段代码将执行循环。您遇到的问题是重新使用相同流的readline并且正在读取EOF(和/或)以创建文件锁争用。
Const TristateFalse = 0
Const ForReading = 1

strINIfile = "c:scriptstestconfig.ini"
strSection = "[Section2]"
strItem = "param3"

Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set objINIstream = objFSO.OpenTextFile(strINIfile, ForReading, False, TristateFalse)

boolCheckSection = False

Do While Not objINIstream.AtEndOfStream
    strLine = Trim(objINIstream.ReadLine)

    ' Determine if we're in the section, set boolcheck if so
    If InStr(UCase(strLine),UCase(strSection)) Then     
        boolCheckSection = True     
    ElseIf InStr(UCase(strLine), "[") Then  
        boolCheckSection = False
    End If

    ' If so, read the proper value and store in variable
    If boolCheckSection Then
        If InStr(UCase(strLine), UCase(strItem)) Then
            tmpParam = Split(strLine, "=")
            strParamValue = Trim(tmpParam(1))
            Exit Do ' exit loop with set variable
        End If      
    End If

Loop

WScript.Echo strParamValue
    
函数ReadIni(myFilePath,mySection,myKey)     '此函数返回从INI文件读取的值     “     '论点:     'myFilePath [string] INI文件的(路径和)文件名     'mySection [string]要搜索的INI文件中的部分     'myKey [string]要返回其值的键     “     '返回:     '指定部分中指定键的[string]值     “
Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

Dim intEqualPos
Dim objFSO, objIniFile
Dim strFilePath, strKey, strLeftString, strLine, strSection

Set objFSO = CreateObject( "Scripting.FileSystemObject" )

ReadIni     = ""
strFilePath = Trim( myFilePath )
strSection  = Trim( mySection )
strKey      = Trim( myKey )

If objFSO.FileExists( strFilePath ) Then
    Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
    Do While objIniFile.AtEndOfStream = False
        strLine = Trim( objIniFile.ReadLine )

        ' Check if section is found in the current line
        If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
            strLine = Trim( objIniFile.ReadLine )

            ' Parse lines until the next section is reached
            Do While Left( strLine, 1 ) <> "["
                ' Find position of equal sign in the line
                intEqualPos = InStr( 1, strLine, "=", 1 )
                If intEqualPos > 0 Then
                    strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
                    ' Check if item is found in the current line
                    If LCase( strLeftString ) = LCase( strKey ) Then
                        ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
                        ' In case the item exists but value is blank
                        If ReadIni = "" Then
                            ReadIni = " "
                        End If
                        ' Abort loop when item is found
                        Exit Do
                    End If
                End If

                ' Abort if the end of the INI file is reached
                If objIniFile.AtEndOfStream Then Exit Do

                ' Continue with next line
                strLine = Trim( objIniFile.ReadLine )
            Loop
        Exit Do
        End If
    Loop
    objIniFile.Close
Else
    WScript.Echo strFilePath & " doesn't exists. Exiting..."
    Wscript.Quit 1
End If
结束功能     
感谢大家的回复。查看你提出的代码并研究它们让我意识到我自己的代码首先没有任何问题(即使有2个while循环,它正确地退出它们)。在尝试了你提出的代码之后,我仍然发现它在我的测试用例中随机断言并在考虑之后我意识到我正在测试的主要应用程序仍然访问这个文件(虽然我不知道为了什么,但那是其他的东西)。 我找到的解决方案是我们在手动测试时总是建议的解决方案复制原件并在副本上工作。 另外,感谢您提出的代码,它们给了我不同的角度来改进我的代码设计和可读性。     

要回复问题请先登录注册