Async stream writing in a thread
Posted
by blez
on Stack Overflow
See other posts from Stack Overflow
or by blez
Published on 2010-05-10T16:55:53Z
Indexed on
2010/05/10
17:04 UTC
Read the original article
Hit count: 231
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);
}
}
© Stack Overflow or respective owner