473,769 Members | 5,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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.tx t", True, System.Text.Enc oding.UTF8)
xFile.Write(Now )
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOEx ception' 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 2850
In article <DD************ *************** *******@microso ft.com>,
He****@discussi ons.microsoft.c om 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.tx t", True, System.Text.Enc oding.UTF8)
xFile.Write(Now )
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOEx ception' 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************ *************** *******@microso ft.com>,
He****@discussi ons.microsoft.c om 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.tx t", True, System.Text.Enc oding.UTF8)
xFile.Write(Now )
xFile.Flush()
xFile.Close()

The error:
An unhandled exception of type 'System.IO.IOEx ception' 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
715
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 anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
2
2912
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 successfuly, I saw two files ".mdb" and ".ldb" in my temp folder. I have no problem to open the database manually. But when I'm tring to use Response.WriteFile() to download database file to end user's local machine, I kept getting the following
2
355
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: Dim xFile As StreamWriter
7
23189
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 response.Redirect to that URL. The problem is that whenever I run the page the script fails on the File.Copy() command everytime with an error of: The process cannot access the file
9
1802
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 where the object is referenced. In Notes, I'd just get a synopsis which shows all executable lines of code in the entire database. Does Access have a similar function?
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.