471,350 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

"Process/thread cannot access file" problem

Hi

Im trying to make an app that uses threading, I want to write to a file in a
loop.

How can i ensure that only one thread writes to the file a the time???

Im accessing the fil with:

Dim xFile As StreamWriter
xFile = New StreamWriter("C:\threadtest.txt", True, System.Text.Encoding.UTF8)
xFile.Write(Now)
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll
Additional information: The process cannot access the file
"C:\threadtest.txt" because it is being used by another process.
Jul 21 '05 #1
2 2705
In article <DD**********************************@microsoft.co m>,
He****@discussions.microsoft.com says...
Hi

Im trying to make an app that uses threading, I want to write to a file in a
loop.

How can i ensure that only one thread writes to the file a the time???

Im accessing the fil with:

Dim xFile As StreamWriter
xFile = New StreamWriter("C:\threadtest.txt", True, System.Text.Encoding.UTF8)
xFile.Write(Now)
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll
Additional information: The process cannot access the file
"C:\threadtest.txt" because it is being used by another process.

In C# there is lock() keyword. I do not know the equivalent in VB.Net,
but actually lock is just a syntax sugar for Monitor.Enter and
Monitor.Exit. The real code is something like:

monitor.Enter();
try
{
do my job
}
finally
{
monitor.Exit
}

I know that in VB.New there is a similar construct for
try/catch/finally, so if there is not a lock equivalent, you can do it
with something like that.

Sunny
Jul 21 '05 #2
Thanks Sunny,,

It's pretty much the same in vb,, i will now try this...

"Sunny" wrote:
In article <DD**********************************@microsoft.co m>,
He****@discussions.microsoft.com says...
Hi

Im trying to make an app that uses threading, I want to write to a file in a
loop.

How can i ensure that only one thread writes to the file a the time???

Im accessing the fil with:

Dim xFile As StreamWriter
xFile = New StreamWriter("C:\threadtest.txt", True, System.Text.Encoding.UTF8)
xFile.Write(Now)
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll
Additional information: The process cannot access the file
"C:\threadtest.txt" because it is being used by another process.

In C# there is lock() keyword. I do not know the equivalent in VB.Net,
but actually lock is just a syntax sugar for Monitor.Enter and
Monitor.Exit. The real code is something like:

monitor.Enter();
try
{
do my job
}
finally
{
monitor.Exit
}

I know that in VB.New there is a similar construct for
try/catch/finally, so if there is not a lock equivalent, you can do it
with something like that.

Sunny

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by mvsguy | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.