ADv. Thread Question

V

Vai2000

Hi All, I am running into a concurrency problem. I have a worker process
which reads some data out of a file and inserts fresh data into it. I have
put a Monitor.Enter(this) and Monitor.Exit(this) on both of the routines
which do read and writes.
The worker process is spawned off by a Multithreaded application. I am
seeing error in log which tells me that
Description:
Stack Trace- at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at System.Data.DataSet.WriteXml(String fileName, XmlWriteMode mode)
at System.Data.DataSet.WriteXml(String fileName)
Message-The process cannot access the file "C:\WUTemp\foocache.xml" because
it is being used by another process.
Inner Exception-
Source-mscorlib
Base Exception Message-The process cannot access the file
"C:\WUTemp\foocache.xml" because it is being used by another process.
Base Stack Trace- at System.IO.__Error.WinIOError(Int32 errorCode, String
str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at System.Data.DataSet.WriteXml(String fileName, XmlWriteMode mode)
at System.Data.DataSet.WriteXml(String fileName)
Base Source-mscorlib

Earlier I tried using the Reader and Writer locks too...but ran into timeout
errors...

How do Rectify this?

TIA
 
A

AlexS

Hi, Vai2000

It looks like you don not close file when using it - basically that means
you are trying to access same file from at least 2 different threads and
second thread cannot get access. Check where you open file before entering
monitor and if you close it properly before exiting.

HTH
Alex
 
J

Jon Skeet [C# MVP]

Vai2000 said:
Hi All, I am running into a concurrency problem. I have a worker process
which reads some data out of a file and inserts fresh data into it. I have
put a Monitor.Enter(this) and Monitor.Exit(this) on both of the routines
which do read and writes.

It's a bad idea to lock on "this" - see
http://www.pobox.com/~skeet/csharp/multithreading.html#lock.choice
The worker process is spawned off by a Multithreaded application. I am
seeing error in log which tells me that
Description:
Stack Trace- at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at System.Data.DataSet.WriteXml(String fileName, XmlWriteMode mode)
at System.Data.DataSet.WriteXml(String fileName)
Message-The process cannot access the file "C:\WUTemp\foocache.xml" because
it is being used by another process.

Right. That suggests that you haven't closed the file earlier - or that
a different process is indeed using it, or that another thread is
trying to use it at the same time.
 
V

Vai2000

Here's little more scoop to the Problem.
--- ProcessA calls x number of WorkerThreads which try to access a file for
reading and writing.
-- Main Process can potentially Launch x number of ProcessA

How do I guard the resource...hack way is to put the stuff in DB and read
and write off the DB but I don't want any R/T over the wire!!!

Temp solution was that I have applied delay on the Main process which calls
ProcessA and at this time ProcessA is launching only 1 Thread.

But need some solid solution.

Appreciate all ur inputs


TIA
 
W

Willy Denoyette [MVP]

Guard the resource with a mutex, or better use a queue to post the IO
requests from your worker threads to a single thread to do your 'shared'
file IO.

Willy.

Vai2000 said:
Here's little more scoop to the Problem.
--- ProcessA calls x number of WorkerThreads which try to access a file
for
reading and writing.
-- Main Process can potentially Launch x number of ProcessA

How do I guard the resource...hack way is to put the stuff in DB and read
and write off the DB but I don't want any R/T over the wire!!!

Temp solution was that I have applied delay on the Main process which
calls
ProcessA and at this time ProcessA is launching only 1 Thread.

But need some solid solution.

Appreciate all ur inputs


TIA
 
V

Vai2000

thanks, do you mean MSMQ ? can you bring some more insight on this.

TIA

Willy Denoyette said:
Guard the resource with a mutex, or better use a queue to post the IO
requests from your worker threads to a single thread to do your 'shared'
file IO.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top