Async stream writing in a thread
- by blez
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);
}
}