简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM