Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>FileStream</code> will allow you to seek to the portion of the file you want, no problem. It's the recommended way to do it in C#, and it's fast.</p> <p>Sharing between threads: You will need to create a lock to prevent other threads from changing the FileStream position while you're trying to read from it. The simplest way to do this:</p> <pre><code>// This really needs to be a member-level variable; private static readonly object fsLock = new object(); // Instantiate this in a static constructor or initialize() method private static FileStream fs = new FileStream("myFile.txt", FileMode.Open); public string ReadFile(int fileOffset) { byte[] buffer = new byte[bufferSize]; int arrayOffset = 0; lock (fsLock) { fs.Seek(fileOffset, SeekOrigin.Begin); int numBytesRead = fs.Read(bytes, arrayOffset , bufferSize); // Typically used if you're in a loop, reading blocks at a time arrayOffset += numBytesRead; } // Do what you want to the byte array and return it } </code></pre> <p>Add <code>try..catch</code> statements and other code as necessary. Everywhere you access this FileStream, put a lock on the member-level variable fsLock... this will keep other methods from reading/manipulating the file pointer while you're trying to read.</p> <p>Speed-wise, I think you'll find you're limited by disk access speeds, not code.</p> <p>You'll have to think through all the issues about multi-threaded file access... who intializes/opens the file, who closes it, etc. There's a lot of ground to cover.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload