![]() |
| | #1 |
| Registered User Join Date: Aug 2001 Location: UK
Posts: 339
| TextReader.Synchronized 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 | |
| | #2 |
| Registered User 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 | |
| | #3 |
| Registered User 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 | |
| | #4 |
| Registered User 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 | |
| | #5 |
| Registered User 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 | |
| | #6 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|