tags:

views:

214

answers:

2

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?

A: 

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

JaredPar
+1  A: 

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)

Bogdan Maxim
ya, i was hoping the io stream could be written to by multiple threads given i know the pointer and the files's size, doesnt work that way it seems
Jim
It seems that there could be a solution using a FileStream. I'm checking it right now and let you know
Bogdan Maxim
you use buffers to write to the stream, the problem is like acessing a control that has been created on a diffrent thread, and im only writing to part of that thread, ill try a few things and then we shall see, im thinking of loading the files into memory then using the buffer to write to a defined area
Jim