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

"Process/thread cannot access file" problem

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.