How to make asynchronous call in wpf application?

Leave a Comment
Make asynchronous  call example in wpf  application. here i will saw you that you will crate asynchronous call in it . I will give you complete source code as well as code snippets here. So let's start,

 Main Window
<Window x:Class="AsynchronusExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Text="This is task from asynchronuc call"  Margin="20,20,300,268"/>
        <Label Content="" Name="label1" HorizontalAlignment="Left" Margin="222,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="-4.325,-3.969" Height="32" Width="114"/>
<Button Height="40" Content="Check with asychronus" Margin="179,78,184,202" Click="Button_Click_1"/>
    </Grid>
</Window>
MainWindow.cs
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 AsynchronusExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            asynccallingmethod();
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("I am clicking with parallel in asynchronus mode");
        }
        private async void asynccallingmethod()
        {
            int sum = 0;
            for (; ; )
            {
                await Task.Run(() =>
                    {
                     
                        sum++;
                    });
                label1.Content = sum.ToString();
            }
     
        }
    }
}
Output of above exampl is look like,


0 comments:

Post a Comment