0

ログファイルに書き込むいくつかの並列プロセスを持つプログラムがあります。メインプログラムもログを読み取り、テキストフィールドにプールしています。

問題は、特定のポイントに到達すると、プロセスが書き込みを続けているにもかかわらず、ログを見逃して常に 0 バイトを読み取ることです。これは私のリーダーサブです。しばらくすると、行Dim n As Integer = stream.Read(bytes, numBytesRead, numBytesToRead)は常に0になります:

Public Function readFile(ByRef stream As FileStream) As String
    Dim bytes() As Byte = New Byte((stream.Length) - 1) {}
    Dim numBytesToRead As Integer = CType(stream.Length, Integer)

    Dim numBytesRead As Integer = 0
    While (numBytesToRead > 0)
        ' Read may return anything from 0 to numBytesToRead. 
        Dim n As Integer = stream.Read(bytes, numBytesRead, numBytesToRead)
        ' Break when the end of the file is reached. 
        If (n = 0) Then
            Exit While
        End If
        numBytesRead = (numBytesRead + n)
        numBytesToRead = (numBytesToRead - n)

    End While
    numBytesToRead = bytes.Length

    Return Encoding.UTF8.GetString(bytes)
End Function

streamパラメーターは、次のように宣言されたグローバル変数 (メインの Winform に対してグローバル) です。

Private FSReaderLog As FileStream

次に示すように初期化 (フォーム ロードのみ) し、ストリームとトランキング ファイルのコンテンツを開きます。

Public Function InitLog(ByVal ruta_fichero As String) As FileStream
    Dim FS As New FileStream(ruta_fichero, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
    FS.SetLength(0)
    FS.Flush()
    Return FS
End Function

読者の何が間違っている可能性がありますか?

ありがとうございました

4

0 に答える 0