views:

53

answers:

1

I have a thread in which I write to 2 streams. The problem is that the thread is blocked until the first one finishes writing (until all data is transferred on the other side of the pipe), and I don't want that. Is there a way to make it asynchronous? chunkOutput is a Dictionary filled with data from multiple threads, so the faster checking for existing keys is, the faster the pipe will write.

void ConsumerMethod(object totalChunks) {
    while(true) {
        if (chunkOutput.ContainsKey(curChunk)) {
            if (outputStream != null && chunkOutput[curChunk].Length > 0) {
                outputStream.Write(chunkOutput[curChunk]); // <-- here it stops
            }

            ChunkDownloader.AppendData("outfile.dat",
                    chunkOutput[curChunk], chunkOutput[curChunk].Length);

            curChunk++;
            if (curChunk >= (int) totalChunks) return;
        }

        Thread.Sleep(10);
    }            
}
A: 

You can use the Async methods of Stream, such as BeginWrite. You can check the IAsyncResult returned to detect when the write has finished.

driis
And if it's not finished, until the next loop? I want to send the bytes to the pipe and continue with my code
blez