Rafael Osuna Dominguez
Rafael Osuna Dominguez

Reputation: 479

Read and write a text file at same time using VB.NET

I've got a program that must read a codebar and write the read code in a text file. Every 30 seconds a thread is launched that read that file to do some operations and delete the lines already processed.

My problem is that while I'm reading the file from the main thread, it has to be able to write more lines and be read later for the thread launched every 30 seconds.

I really haven't find any sample or idea of how approach the problem. Is there a solution?

Upvotes: 0

Views: 1294

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25023

Instead of using a file as the backing store for the work to be done, how about using a Queue for the "live" data? You could still use a file as a log, putting in entries for when items are queued and removed from the queue in case you need to refer back to the history of actions. That way, the file only needs to be appended to and never read, unless a roll-back of some sort is required, for example your program was stopped at an inopportune moment.

Upvotes: 1

user858224
user858224

Reputation:

When you have a shared mutable resource like this file and multiple units accessing it, it is common to use a lock. Locks can exist within a program, but is also offered by many file systems. Lock the file when you need to access it, and release the lock when you don't need it anymore. That way only one part of your program will access the file at any given point.

Upvotes: 1

Related Questions