• ### Popular Now

• 13
• 16
• 27
• 9
• 9

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm in the midst of alpha testing of my MMORPG Fleurin (Actually, its being done right now, but the server just crashed with a bunch of players online and I'm not sure how to fix it. The problem is... My webserver and my game-server both tried to access a file at relatively the same time. And it threw an IOException(C#). I have no clue how to fix this, any ideas? (Yes, I'm under alot of pressure atm)

##### Share on other sites
I would say run a check to see if the file is already open before trying to open it, but I'm not sure this can be done if you're talking about two seperate programs attempting to access the same file at the same time. It seems like it would be hard to stop two seperate programs from trying to perform different operations on the same file at the same time.

So yes, to answer your question, I really don't have all that much to add in the way of constructive help, but this is a question I'd like to know an answer to, so I thought I'd throw my two cents in there.

Vopisk

##### Share on other sites
I have a cast of people waiting for me to "fix this and restart the server", so I appreciate any ideas I can get.

Checking the access sounds like a tangible idea. I'll do some googling for functions to implement the lock check.

Edit: I can't find any such functions in C#.

##### Share on other sites
Mutexes work across processes (you just have to give them a unique name that's the same in both apps - I use GUIDs)

##### Share on other sites
I did this...

        /* Set the text writer to output. */        //MyTextReader = new StreamReader(TheFileName); /* <-- Old code */        MyFS = new FileStream(TheFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);        MyTextReader = new StreamReader(MyFS);

... and hoping that it by setting the FileShare property, that it will let me have the server write to the file while the web server reads it.

At the very least, I have a few minutes to do some research before the server crashes again.

@Nypyren
Thanks, I'll look into that right now.

##### Share on other sites
Good call on the mutexes Nypyren, a quick google turned up a wealth of information and this is the obvious answer to the problem at hand. To the OP it still is not going to let you read and write at the same time, files don't like to be shared between two programs at once generally in my experience, unless the program is actually just making a copy of the file and doing something to it, closing the actual file to free it up for another program/function to access it. But at least it should keep you from crashing and at most I'd figure you're looking at a momentary hitch in your giddy-up.

##### Share on other sites
lol have one process poll the other process to access the file!! yeah! lol dont listen to me btw :P

##### Share on other sites
Is it important that I mention that only one process needs to write/read to the file; the other process only reads?

##### Share on other sites
Guys, it would appear that the FileShare attribute allows multiple processes to read/write to the file.

Have a look.. System.IO.FileShare

Although multiple writes would be very troublesome, the fact that only one of my threads does any writing means this may actually work.

Can anyone with experience in this field verify it for me?

##### Share on other sites
When you're writing to the file, are you appending, or are you rewriting all the data in the file?

Either way, you'll have to be sure not to let the readers read data that's only partially written. That's where the mutex might come in handy - although if you have more than two processes, you might need one of the other syncronization classes in case you want to have multiple readers able to read without waiting for a "single" resource like a mutex. (although I've heard ReaderWriterLock sucks, I don't have any experience using it).

(Other than that, the FileShare.ReadWrite will probably work fine if you make sure your write buffer is flushed or the stream is closed before readers are allowed to read).