The process cannot access the file ...

09/05/2014 18:28 FromHell2Kill#1
While i'm using the source and testing i have got a console Error tells me that :

The process cannot access the file 'D:\Games\ConquerSource\MINE\AboDiab\5936\bin\Debu g\database\Jianghu.txt' because it's being used by another process.

And it gives me some information like :
Jianghu.cs line 147
Write.cs line 54

But i had ignored this console error.

Unfortunately, When i left the source running for about an hour .. the server stopped and gives me that error.

[Only registered and activated users can see links. Click Here To Register...]

How can i solve this annoying problem?
09/05/2014 18:43 CptSky#2
Mhm...

So you write an empty string to a file, instead of creating it directly. Than, you truncate it and close it ? Than, you write data to it ? Honestly, your code is totally wrong. If you want to overwrite the file, just open it in write mode instead of append. All that can be done with a single stream.

Anyway, I suppose that another process is already locking the file, or that File.WriteAllText is not releasing the handle directly after writing the data.
09/05/2014 19:08 nTL3fTy#3
If you can't even use the File class properly, why are you trying to run a server...
09/05/2014 19:18 FromHell2Kill#4
@CptSky --> Well, how can i write that code in the standard way?

Or specifically, How can i repair this problem?

i tried alot and already copied the Jianghu.txt from another source then a pasted it to mine, i got the same error!!!

I respect you somehow ^_^.

_________________________________
@nTL3fTy --> Firstly, it's not a published server. Just a local one for me, Just practicing on it.

Secondly, not even your business.
:)
09/05/2014 19:44 nTL3fTy#5
That little piece of code seems to use 3 file streams to do something you could achieve with one line of code (and one stream) -- already explained by CptSky. If you can't even work with the basic libraries provided to you with full documentation in C#, how do you plan on working on a server with little to no documentation? :rolleyes:
09/05/2014 19:51 FromHell2Kill#6
@nTL3fTy

Thanks for your note ^_^
09/05/2014 20:03 iliveoncaffiene#7
Quote:
Originally Posted by FromHell2Kill View Post
@CptSky --> Well, how can i write that code in the standard way?

Or specifically, How can i repair this problem?

i tried alot and already copied the Jianghu.txt from another source then a pasted it to mine, i got the same error!!!

I respect you somehow ^_^.

_________________________________
@nTL3fTy --> Firstly, it's not a published server. Just a local one for me, Just practicing on it.

Secondly, not even your business.
:)
Firstly, some variable names look like what you get when you use ILSpy.
'writer2' isn't even used, did you code this?
You compare bool == bool, and it isn't even File.Exists == true, you define a variable for it (another bad sign of ILSpy).
When using 'using', you shouldn't need to dispose of or close your writers, that's the point of 'using'.

Here is literally all of that in many less lines:
Code:
            if (mod == Conquer_Online_Server.Database.Mode.Open)
            {
                if (File.Exists(this.location))
                {
                    using (var writer = File.OpenWrite(this.location))
                    {
                        writer.WriteLine("Count={0}", this.Count);
                        foreach (var it in this.Items)
                            writer.WriteLine(it);
                    }
                }

                // whatever else you have here....
            }
09/05/2014 23:02 abdoumatrix#8
this might be better


--------------------------

the problem is that ur source save the whole jiang client every entity close.

u need only one save on exit


check the code all reference and u will see
09/06/2014 01:12 iliveoncaffiene#9
Quote:
Originally Posted by abdoumatrix View Post
this might be better


--------------------------

the problem is that ur source save the whole jiang client every entity close.

u need only one save on exit


check the code all reference and u will see
You do realize the point of "using" don't you?! It's to auto initialize and dispose of a disposable object - that includes a StreamWriter. ".Close()" does not need to be called, the end of the "using" block does that automatically.
Also, assuming Items is an array, you should use foreach since you are not using the indexes for anything.
Also, you could condense it more and do:
Code:
using (var SW = (mod == Mode.Open ? new StreamWriter(location, false, Encoding.Default) : 
new StreamWriter(File.Create(location))))
and just have 1 loop. Eliminates so many lines of repeated code
09/06/2014 19:58 Xio.#10
Quote:
Originally Posted by iliveoncaffiene View Post
You do realize the point of "using" don't you?! It's to auto initialize and dispose of a disposable object - that includes a StreamWriter. ".Close()" does not need to be called, the end of the "using" block does that automatically.
Also, assuming Items is an array, you should use foreach since you are not using the indexes for anything.
Also, you could condense it more and do:
Code:
using (var SW = (mod == Mode.Open ? new StreamWriter(location, false, Encoding.Default) : 
new StreamWriter(File.Create(location))))
and just have 1 loop. Eliminates so many lines of repeated code
Eliminates readability too imo.
09/06/2014 20:06 Spirited#11
Edit: Nevermind, I read the code again for what it does, and the style is ... alright. Completely unnecessary though. Not bad style too much, just bad programming.
09/06/2014 20:16 abdoumatrix#12
Quote:
Originally Posted by iliveoncaffiene View Post
You do realize the point of "using" don't you?! It's to auto initialize and dispose of a disposable object - that includes a StreamWriter. ".Close()" does not need to be called, the end of the "using" block does that automatically.
Also, assuming Items is an array, you should use foreach since you are not using the indexes for anything.
Also, you could condense it more and do:
Code:
using (var SW = (mod == Mode.Open ? new StreamWriter(location, false, Encoding.Default) : 
new StreamWriter(File.Create(location))))
and just have 1 loop. Eliminates so many lines of repeated code
aha i know
just edited the worst part and forget the rest. :D

anyway as said before

the problem isn't from here.

he had to check all the reference that use this

he will find it in a loop when i should recall only 1 time :D

Quote:
Originally Posted by Spirited View Post
I agree with Yuki. Putting a bunch of equals on one line is really bad style. Try to avoid it at all costs. Line count isn't everything.
:confused: :confused:
09/06/2014 20:23 Spirited#13
Also, why are you still reading the file like that? Just read and write to it asynchronously. But wait - why the hell is it a text file? Why aren't you doing this in SQL? Why flat-file?
09/12/2014 03:49 { Angelius }#14
Quote:
Originally Posted by iliveoncaffiene View Post
Code:
using (var SW = (mod == Mode.Open ? new StreamWriter(location, false, Encoding.Default) : 
new StreamWriter(File.Create(location))))
If you must...
Code:
using (var SW = new StreamWriter(new FileStream(Path, FileMode.OpenOrCreate)))
Quote:
Originally Posted by Xio. View Post
Eliminates readability too imo.
Come again!!! Eliminates what?
09/12/2014 03:52 Xio.#15
Readability. English motherfucker, do you speak it? (Was going to put the meme but too few posts, imagine Samuel L Jackson)