user6146147
user6146147

Reputation:

Saving XML file “on the fly” in c#

I work on an app where user can type in some text. Text is saved to XML file, I try to make the file save “on the fly” as user is typing so it saves instantly. However if data is typed quick, I get an error of “file currently in use”. How to overcome this issue?

Upvotes: 0

Views: 130

Answers (2)

Nick Goloborodko
Nick Goloborodko

Reputation: 3053

The reason for the error is that you are trying to write a file while the previous write operation is incomplete and the file is still open for write.

Now, if you absolutely must make a write on every character change - I would put in a queue in place, so when XML content is changed - instead of writing to a file right away - agg a message to a queue. Then have the code that monitors that queue and only writes the next chnage once the previous write has finished.

Upvotes: 1

Rodrigo Machado
Rodrigo Machado

Reputation: 107

You can try to put a flag to control if the file is already open or not. If this is open, you keep the text and don't write on XML, but if it is not you just write.

This is a concurrency problem, you can acess the website: https://www.oreilly.com/library/view/concurrency-in-c/9781491906675/ch01.html to get more options.

Upvotes: 0

Related Questions