By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Stream Writer

P: n/a
Hi,

IF i want to write a line at the same time in append mode how m i suppose to
do.

here is my code.... and this function may be called more then once at the
same time....

and it works fine... but the only thing is that.. sometime i get the error :

The process cannot access the file "c:\nie\LOGS\1062005.txt" because it is
being used by another process.

and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.

below is the function

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)

Dim StreamWriter As New System.IO.StreamWriter(Path & FileName, True)

StreamWriter.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
StreamWriter.Close()
End Function

Any help?

Cheers
C
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Gujju" <Gu***@discussions.microsoft.com> schrieb:
and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.


It won't overlap if it's called on the same thread.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
Hi,

Try using synclock to prevent more than one of the functions
from running at a time. It is not the best idea to use the type name as the
variable name.

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)
synclock filename
Dim SW As New System.IO.StreamWriter(Path & FileName, True)

SW.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
SW.Close()
end synclock

End Function

http://msdn.microsoft.com/library/de...tmsynclock.asp

Ken
--------------------------
"Gujju" <Gu***@discussions.microsoft.com> wrote in message
news:C8**********************************@microsof t.com...
Hi,

IF i want to write a line at the same time in append mode how m i suppose to
do.

here is my code.... and this function may be called more then once at the
same time....

and it works fine... but the only thing is that.. sometime i get the error :

The process cannot access the file "c:\nie\LOGS\1062005.txt" because it is
being used by another process.

and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.

below is the function

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)

Dim StreamWriter As New System.IO.StreamWriter(Path & FileName, True)

StreamWriter.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
StreamWriter.Close()
End Function

Any help?

Cheers
C
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.