C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-13-2009, 05:44 AM   #1
Registered User
 
(TNT)'s Avatar
 
Join Date: Aug 2001
Location: UK
Posts: 339
TextReader.Synchronized

Hey guys,

I'm just wondering how to use a TextReader in different a different process/thread on the same file in a thread safe way, just so i don't get screwed if the files already open for example.

TextReader.Synchronized() looks like what im after, but I was wondering if i have to wrap it around the same TextReader object? Or can i create a new TextReader object, make a TextReader.Synchronized wrapper and it will just block until the file is ok to read from? (And repeat in another thread with new TextReader & TextReader.Synchronized instances)

Thanks,

Jack
__________________
TNT
You Can Stop Me, But You Cant Stop Us All
(TNT) is offline   Reply With Quote
Old 03-13-2009, 08:17 AM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
I've never used TextReader.Synchronized...but you could just as easily wrap your reads/writes in a lock() block. Or for cross process synchronization a mutex.
valaris is offline   Reply With Quote
Old 03-13-2009, 08:21 AM   #3
Registered User
 
(TNT)'s Avatar
 
Join Date: Aug 2001
Location: UK
Posts: 339
Thanks, problem is i dont have any way of cross-thread/inter-process communication.

Im working with WCF and each consumer of a service needs to be able to read the file, then there's multiple services as well, so I think this rules out mutex use sadly because i cant communicate between the services and consumer instances?
__________________
TNT
You Can Stop Me, But You Cant Stop Us All
(TNT) is offline   Reply With Quote
Old 03-13-2009, 08:36 AM   #4
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
Well if all they need to do is read the file you need no synchronization. Each client will have there own instance of some file handle and there own position within the file. Furthermore since nobody is writing to this data, it won't change and your readers can read stable data.
valaris is offline   Reply With Quote
Old 03-13-2009, 08:43 AM   #5
Registered User
 
(TNT)'s Avatar
 
Join Date: Aug 2001
Location: UK
Posts: 339
Ok thanks, sorry I didn't think it would be relevant to mention but I will be writing to the file with one of the WCF services as well.

But would what you said still apply?

The file handle only needs to be valid for a single read/write operation per service instance? So I literally open the file write one line and close it, while in another instance it may try to open the file and read from it, then close it again. Would this require synchronization? I was simply concerned that when I open a file handle in one thread it would lock the file, causing the other thread to fail.

Thanks again for the help
__________________
TNT
You Can Stop Me, But You Cant Stop Us All
(TNT) is offline   Reply With Quote
Old 03-13-2009, 08:46 AM   #6
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
If this is all running in one machine then there are options for opening a file with certain "sharing" rights. IE, no other process can open this for reading while I have it open etc. This way you can have other clients block to wait to open this file until another file closes it.
valaris is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:53 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22