StreamWriter Write error in thread

F

fniles

I am using VB.NET 2005.
My application has a main program and a thread called myThread.
I need to write to a log file in both the main program and the thread, so I
open a file called swLog in the main program and pass along swLog to the
thread.
In order for the application to write to the physical file, in the main
program I have a timer TimerFile with Interval 60000 (1 menit). Every time
the timer kicks in, I close the file and open it again. Then, I will pass
swLog to the Thread again.
Every now and then I will get an error in the swLog.Write method.
At first I will get the error "Count cannot be less than zero."
Then, I will get the error "Value cannot be null. Parameter name:
destination"
Then, at the sub CloseFile in the main program I will get the error "Index
and count must refer to a location within the buffer."
What causes the above errors and is there a better way to do what I need to
do ?
I would like to avoid opening and closing the file everytime I write to it,
that's why I keep the swLog file open to be written and write it every 1
minute.
Thank you very much.

Imports System.IO
Public Class frmTest
Dim swLog As StreamWriter = Nothing
Private BSession As myThread = Nothing
Private BWorkerThread As Threading.Thread = Nothing

frmTest_Load(..)
swLog = New StreamWriter("c:\log\log.txt", True)
TimerFile.Start()
end sub

Public Sub LogonThread()
BSession = New myThread
BWorkerThread = New Threading.Thread(AddressOf BSession.ThreadMain)
BSession.MainForm = Me
BSession.swLog = swLog
BWorkerThread.Name = "BWorkerThread"
BWorkerThread.Start()
end Sub
: --> somewhere in the main program I do swLog.write

Private Sub TimerFile_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TimerFile.Tick
CloseFile()
End Sub

Sub CloseFile()
'--->> ERROR here also "Index and count must refer to a location within the
buffer."
If Not (swLog Is Nothing) Then
swLog.Close()
swLog = Nothing
swLog = New StreamWriter(m_sLogFileName, True)
End If
If Not (BSession Is Nothing) Then BSession.swLog = swLog
end sub
End Class
***********************************************************************************************
Imports System.IO
Public Class myThread
Public MainForm As frmTest = Nothing
Public swLog As StreamWriter

Public Sub ThreadMain()
Dim swError As StreamWriter

Try
swLog.Write("this is a test message" & vbCrLf) '---> ERROR HERE "Count
cannot be less than zero." Also later "Value cannot be null. Parameter name:
destination"
Catch ex3 As Exception
Try
swError = New StreamWriter(Application.StartupPath & "\ESErrorLog-B" &
messageCount & "-" & Date.Now.ToString("MMddyy") & ".txt", True)
swError.Write(Now & " myThread- ThreadMain - error = " & ex3.Message &
vbCrLf)
swError.Close()
swError = Nothing
Catch ex4 As Exception
End Try
End Try
end sub
End Class
 
F

fniles

I think I figure out my problem.
I do not need to close and reopen the file every 1 minute. swLog will indeed
write it to the physical file.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top