SHOW:
|
|
- or go back to the newest paste.
| 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]; |
| 8 | + | var tasks = outputStreams.Select(x => Task.Run(() => splitStream.GetForwardReadOnlyStream().CopyToAsync(x))).ToList(); |
| 9 | - | Task[] tasks = new Task[outputStreams.Length]; |
| 9 | + | |
| 10 | - | for (int i = 0; i < outputStreams.Length; i++) |
| 10 | + | |
| 11 | - | {
|
| 11 | + | |
| 12 | - | readable[i] = splitStream.GetForwardReadOnlyStream(); |
| 12 | + | |
| 13 | - | tasks[i] = Task.Run(() => readable[i].CopyToAsync(outputStreams[i])); |
| 13 | + |