Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After finishing year one at A-level. This holiday I have tried to code many different things. Trying to widening my knowledge!

I was thinking last night how applications such as msn or facebook work around the messaging side. I was to try and create an app that two people could use to talk to one another!. (though reading a file and saving the file)


I had a timer with an interval of 1 second. Every tick I would start a streamreader read the lines and close.

Editing the file adding different lines I could save and most of the time it would work fine. The program would be able to read the data and display it. (done through notepad, for the time being)

Though sometimes when trying to save a file it would see that its being used by another process. Is there a better way of doing this basic program. I could use a 'try' but that's too easy. How would an advanced programmer get over this? Thank you!

P.S My first question! Code Project is quite a nice website!

Summarised question:
Updating a text file, while already being viewed in another.
Posted
Comments
trevorboultwood 16-Aug-13 15:27pm    
VB.NET! Sorry!
ridoy 16-Aug-13 15:58pm    
Welcome to CodeProject!

1 solution

try and catch is the easiest way
Otherwise you might be able to do something else when you know the file is in use (ie. create another file and delete it when done) so the other instance can check if the file exists

Or you can do it through registry... or whatever. You can make it as complex or as simple you want :D
 
Share this answer
 
Comments
trevorboultwood 16-Aug-13 17:43pm    
Brilliant, i did think of that, but just creating a temp file would do I guess! :D Thanks!
Lurker1315 17-Aug-13 11:01am    
Just keep in mind though when they might try to create a temp file at the same time, although chances are even smaller now, it could happen. If it was in the same process/program, you can just use a lock to ask them wait for each other, otherwise I think you'd just have to catch the exception and deal with it later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900