473,507 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread safety issue

In my VB.Net ASP project, I have a function in a module (same thing as a
static function in C#'s terms) that is shared by all the sessions. What is
does once called is to write messages into a common log file that's also
shared by all the sessions. To make sure it is thread safe, I use the Monitor
class to sync it up. Here is the simplified version of that function"

Public Sub WriteLog(ByVal strMsg As String)
Dim oWriter As StreamWriter
Dim strLogPath As String = "C:\Logs\test.log"

If Not File.Exists(strLogPath) Then
oWriter = File.CreateText(strLogPath)
Else
oWriter = File.AppendText(strLogPath)
End If

Monitor.Enter(oWriter)
Try
oWriter.WriteLine(strMsg)
oWriter.Flush()
oWriter.Close()
Finally
Monitor.Exit(oWriter)
End Try
End Sub

My problem is that this code doesn't seem to do what I want it to do. I am
still getting error that says "The process cannot access the file
'C:\Logs\test.log' because it is used by another process.". Why is this
happening? I must be doing something wrong. Can some one tell me how I should
do this?

Thanks a million!

Feng

Nov 19 '05 #1
1 1095
the object you are locking on is local to the call, so threads do not block
each other because they are all locking different objects.

you need to lock on a static object. you could use the class type, but there
is a performance hit for this, better to create an actual object to lock on.
also the File.Exists need to be in lock.

-- bruce (sqlwork.com)

"Feng" <Fe**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
In my VB.Net ASP project, I have a function in a module (same thing as a
static function in C#'s terms) that is shared by all the sessions. What is
does once called is to write messages into a common log file that's also
shared by all the sessions. To make sure it is thread safe, I use the
Monitor
class to sync it up. Here is the simplified version of that function"

Public Sub WriteLog(ByVal strMsg As String)
Dim oWriter As StreamWriter
Dim strLogPath As String = "C:\Logs\test.log"

If Not File.Exists(strLogPath) Then
oWriter = File.CreateText(strLogPath)
Else
oWriter = File.AppendText(strLogPath)
End If

Monitor.Enter(oWriter)
Try
oWriter.WriteLine(strMsg)
oWriter.Flush()
oWriter.Close()
Finally
Monitor.Exit(oWriter)
End Try
End Sub

My problem is that this code doesn't seem to do what I want it to do. I am
still getting error that says "The process cannot access the file
'C:\Logs\test.log' because it is used by another process.". Why is this
happening? I must be doing something wrong. Can some one tell me how I
should
do this?

Thanks a million!

Feng

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6629
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
4
2775
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
7
2292
by: Chad Zalkin | last post by:
We are evaluating some old code that was written as part of our math library. This code uses some optimizations that I'm not sure are necessary or safe, but is a source of debate between my...
17
5308
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from...
11
1362
by: SQL_Learner | last post by:
Hi, I was going through this article http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx This talks about "C++ scoped static initialization is not thread-safe, on purpose!" One of...
6
3125
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
10
1528
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
13
3565
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
2
4033
by: ZHENG Zhong | last post by:
Hi, I implemented a small logging library with the API like this: logger& log = log_manager::instance().get_logger("my_logger"); log.stream(DEBUG) << "this is a debug message" << std::endl;...
44
7742
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.