Exemplo n.º 1
0
        private static async Task Test()
        {
            var             idList      = Enumerable.Range(1, 1000000);
            IDatabaseReader dbReader    = new FakeDatabaseReader(500);
            IDatabaseWriter dbWriter    = new FakeDatabaseWriter(200);
            IQueueWriter    queueWriter = new FakeQueueWriter(25);

            // Note that IAsyncEnumerable has a nifty "WithCancellation" method, which you can just call at the end and
            // it will apply down through the stack. However, you can only do this when the result is an IAsyncEnumerable.
            // In this example, I

            var cancellationToken = new CancellationToken();

            var process = idList.SafeParallelAsyncWithResult(id => dbReader.ReadData(id), 30)
                          .SafeParallelAsyncWithResult(readResult => dbWriter.WriteData(readResult.Output), 100)
                          .SafeParallelAsync(writeResult => queueWriter.Write(writeResult.Output.SomeDescription), 50, cancellationToken);

            await process;
        }
Exemplo n.º 2
0
        private static async Task Test2()
        {
            var dummyList = Enumerable.Range(1, 1000000);

            IDatabaseWriter dbWriter = new FakeDatabaseWriter(200);

            var results = dummyList.SafeParallelAsyncWithResult(number => dbWriter.WriteData(new SourceData(number)), 100);

            await foreach (var result in results)
            {
                if (result.Success)
                {
                    Console.WriteLine($"Wrote {result.Output} to the database");
                }
                else
                {
                    Console.WriteLine($"Failed to write record with id {result.Input}. Error: {result.Exception}");
                }
            }
        }
Exemplo n.º 3
0
        private static async Task Test3()
        {
            var             dummyList = Enumerable.Range(1, 1000000);
            IDatabaseWriter dbWriter  = new FakeDatabaseWriter(200);
            var             results   = dummyList.SafeParallelAsyncWithResult(number => dbWriter.WriteData(new SourceData(number)), 100);

            var cancellationToken = new CancellationToken();

            await foreach (var result in results.WithCancellation(cancellationToken))
            {
                // Do something
            }
        }