Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created May 29, 2017 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miteshsureja/bd8afe77094f06282f8c6cdd9a0b6f94 to your computer and use it in GitHub Desktop.
Save miteshsureja/bd8afe77094f06282f8c6cdd9a0b6f94 to your computer and use it in GitHub Desktop.
Async and Await - Asynchronous Programming
<Window x:Class="AsyncAwaitExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AsyncAwaitExample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="Enter number to sum:" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,50,0,0"/>
<TextBox Name="Input" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0" />
<Button Content="Calculate" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Margin="0,10,0,0" Click="Button_Click"/>
<TextBlock Text="The sum is:" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,25,0,0"/>
<TextBlock Name="Answer" Text="" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0"/>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AsyncAwaitExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public async void DoSum(int number)
{
Console.WriteLine("Task started");
var result = Calculate(number);
Console.WriteLine("Task completed");
if (!result.IsCompleted)
await result;
Answer.Text = result.Result.ToString();
Console.WriteLine(string.Format("Answer is {0}", result.Result.ToString()));
}
public async Task<int> Calculate(int number)
{
var sum = 0;
Task myTask = new Task(() =>
{
for (int i = 1; i <= number; i++)
{
System.Threading.Thread.Sleep(1000);
sum += i;
}
});
myTask.Start();
await myTask;
return sum;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(Input.Text))
{
Console.WriteLine("Sum process started");
var number = Convert.ToInt32(Input.Text);
DoSum(number);
Console.WriteLine("Sum process completed");
}
}
}
}
@miteshsureja
Copy link
Author

Output -

Sum process started
Task started
Task completed
Sum process completed
Answer is 55

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment