473,240 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 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 2811
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
2
by: lucy | last post by:
Hi, I use ADOX to create a Access database programmatically from my asp.net application and save ".mdb" file to a temp folder under my project foler on the web server. After database is created...
2
by: Henrik | last post by:
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: ...
7
by: RSH | last post by:
Hi, I have an ASP .Net web page that creates a temp directory on the server then it is using the File.Copy command to move a file to a temp download directory, Then I am doing a...
9
by: mvsguy | last post by:
I'm a Notes admin tasked with fixing an Access problem. I hope someone will be gracious enough to help. The database is getting a 3420, object not defined, and I need to find all the places...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.