简体   繁体   中英

Multithreading a filestream in vb2005

I am trying to build a resource file for a website basically jamming all the images into a compressed file that is then unpacked on the output buffers to the client.

my question is in vb2005 can a filestream be multi threaded if you know the size of the converted file, ala like a bit torrent and work on pieces of the filestream ( the individual files in this case) and add them to the resource filestream when they are done instead of one at a time?

If you need something similar to the torrents way of writing to a file, this is how I would implement it:

  1. Open a FileStream on Thread T1, and create a queue "monitor" for step 2
  2. Create a queue that will be read from T1, but written by multiple network reader threads. (the queue data structure would look like this: (position where to write, size of data buffer, data buffer).
  3. Fire up the threads

:)

Anyway, from your comments, your problem seems to be another one..

I have found something in, but I'm not sure if it works:

If you want to write data to a file, two parallel methods are available, WriteByte() and Write() . WriteByte() writes a single byte to the stream:

byte NextByte = 100;
fs.WriteByte(NextByte);

Write() , on the other hand, writes out an array of bytes. For instance, if you initialized the ByteArray mentioned before with some values, you could use the following code to write out the first nBytes of the array:

fs.Write(ByteArray, 0, nBytes);

Citation from:

Nagel, Christian, Bill Evjen, Jay Glynn, Morgan Skinner, and Karli Watson. "Chapter 24 - Manipulating Files and the Registry". Professional C# 2005 with .NET 3.0. Wrox Press. © 2007. Books24x7. http://common.books24x7.com/book/id_20568/book.asp (accessed July 22, 2009)

I'm not sure if you're asking if a System.IO.FileStream object can be read from or written to in a multi-threaded fashion. But the answer in both cases is no. This is not a supported scenario. You will need to add some form of locking to ensure serialized access to the resource.

The documentation calls out multi-threaded access to the object as an unsupported scenario

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