private async void computeButton_Click(object sender, EventArgs e) { computeButton.Enabled = false; Computation c = new Computation(); int m = Environment.TickCount % 10 + 20; long r = await c.ComputeAsync(1, m); outputLabel.Text = $"Result = {r}"; computeButton.Enabled = true; }
private async void computeButton_Click(object sender, EventArgs e) { resultLabel.Text = "Computing..."; computeButton.Enabled = false; int n = 20 + Environment.TickCount % 11; Computation c = new Computation(); long r = await c.ComputeAsync(1, n); resultLabel.Text = $"Result = {r}"; computeButton.Enabled = true; }
private async void computeButton_Click(object sender, EventArgs e) { outputLabel.Text = "Busy"; computeButton.Enabled = false; Computation c = new Computation(); int n = 20 + Environment.TickCount % 6; long result = await c.ComputeAsync(1, n); outputLabel.Text = $"Result is {result}"; computeButton.Enabled = true; }
/* * private static Task DoComputation() * { * Console.Write("Computing..."); * * Computation c = new Computation(); * return c.ComputeAsync(1, 20) * .ContinueWith(t => * { * long r = t.Result; * Console.WriteLine("Done!"); * Console.WriteLine("Result = {0}", r); * }); * } */ //this method can return a task using 'await' statement private static async Task DoComputation() { Console.Write("Computing..."); Computation c = new Computation(); long r = await c.ComputeAsync(1, 20); //return the awaited task to the caller and execute the code //below after the awaited task is completed Console.WriteLine("Done!"); Console.WriteLine("Result = {0}", r); }
//This method will return control to the caller using await private async static Task DoComputation() { Console.Write("Computing..."); Computation c = new Computation(); //control is returned to the caller while the awaited task executes long result = await c.ComputeAsync(1, 20); //following code will execute after awaited task is completed Console.WriteLine("Done!"); Console.WriteLine("Result = {0}", result); }
/* * private static Task DoComputation() * { * Console.Write("Computing..."); * * Computation c = new Computation(); * * return c.ComputeAsync(1, 20) * .ContinueWith(t => * { * long r = t.Result; * * Console.WriteLine("Done!"); * Console.WriteLine("Result = {0}", r); * } * ); * } */ // this method will return a task using await statement private static async Task DoComputation() { Console.Write("Computing..."); Computation c = new Computation(); long r = await c.ComputeAsync(1, 20); // will return the awaited task and will continue // the execution of code below after the awaited task completes Console.WriteLine("Done!"); Console.WriteLine("Result = {0}", r); }