Advertisement
domints

Untitled

Sep 22nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. //When I run this method, it is used in Task.WhenAny() with more such instances (I'm trying to tap into streams in proxy to log what's flowing).
  2. //This method throws IndexOutOfRangeException on readable[i].CopyToAsync(outputStreams[i]) - Debugger shows value of 'i' in time of execution is 2 (outputStreams.Length is 2).
  3. //Shouldn't lambda exception store the value of 'i' from time of declaration, instead of finding the current value (which might not exist already)?
  4.  
  5. public static Task MultiplyStream(this Stream inputStream, params Stream[] outputStreams)
  6. {
  7.   var splitStream = new ReadableSplitStream(inputStream);
  8.   Stream[] readable = new Stream[outputStreams.Length];
  9.   Task[] tasks = new Task[outputStreams.Length];
  10.   for (int i = 0; i < outputStreams.Length; i++)
  11.   {
  12.     readable[i] = splitStream.GetForwardReadOnlyStream();
  13.     tasks[i] = Task.Run(() => readable[i].CopyToAsync(outputStreams[i]));
  14.   }
  15.  
  16.   splitStream.StartReadAHead();
  17.  
  18.   return Task.WhenAll(tasks);
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement