예제 #1
0
        private static void RunDemoSynchronous(SampleDataLayer sampleDataLayer)
        {
            // Execute long and short running methods synchronously
            string dataA = sampleDataLayer.GetDelayedApiResponse(ApiWaitTimeSeconds);
            string dataB = sampleDataLayer.SimulateLongProcess(MethodWaitTimeSeconds);
            int    dataC = sampleDataLayer.ShortRunningCalculation();

            SampleData sample = new SampleData(dataA, dataB, dataC);

            Console.WriteLine();
            Console.WriteLine("SampleData object 'sample' created:");
            Console.WriteLine("sample.A = {0}", sample.A);
            Console.WriteLine("sample.B = {0}", sample.B);
            Console.WriteLine("sample.C = {0}", sample.C);
        }
예제 #2
0
        private static async Task RunDemoAsync(SampleDataLayer sampleDataLayer)
        {
            // Create tasks for long running asynchronous methods
            Task <string> taskDataA = sampleDataLayer.GetDelayedApiResponseAsync(ApiWaitTimeSeconds);
            Task <string> taskDataB = sampleDataLayer.SimulateLongProcessAsync(MethodWaitTimeSeconds);

            // Execute short running synchronous method
            int dataC = sampleDataLayer.ShortRunningCalculation();

            // Await the completeion of the tasks
            Console.WriteLine("Awaiting tasks...");
            await Task.WhenAll(taskDataA, taskDataB).ConfigureAwait(false);

            SampleData sample = new SampleData(taskDataA.Result, taskDataB.Result, dataC);

            Console.WriteLine();
            Console.WriteLine($"SampleData object '{nameof(sample)}' created:");
            Console.WriteLine($"sample.A = {sample.A}");
            Console.WriteLine($"sample.B = {sample.B}");
            Console.WriteLine($"sample.C = {sample.C}");
        }