Asynchronous programming has been started with continuation of Dynamic Programming in .NET 4.0 framework with C# , since in .NET Framework 4.5 Client Profile contains a strong support for Asynchronus programming & portrays a full – fleged .NET programming’s future in Asynchronous programming with MultiBinding Support in ASP.Net 4.5 , Strong API with XHTML 5 , CSS 3 , JQuery.
Advantage of Asynchronous Programming: Parallel Programming or Dynamic Programming shows some facilities of Async client call rather than synchronous processing of method. Those of constrains have been throughly modified in .NET Framework 4.5 & enriches the new feature of Asynchronous programming in C# /VB.
Sample Application:
The Sample Application demonstrates the Asynchronous downloading of web: msdn.microsoft.com with left & corresponding page details in right pane of the WPF Windows.
Sample Application Development :
Create a new WPF 4.5 application in Visual Studio 2011 & named in AsyncApp.
Provide the necessary code of MainWindow.xaml:
<Window x:Class=”AsyncLab.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”MainWindow”
Width=”640″ Height=”480″>
<Window.Resources>
<DataTemplate x:Key=”DataTemplateItem”>
<StackPanel Orientation=”Horizontal”>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat=” {0} – {1} “>
<Binding Path=”Length”/>
<Binding Path=”Title”/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<TextBox Height=”379″ HorizontalAlignment=”Left” Margin=”12,12,0,0″ Name=”textBox1″ VerticalAlignment=”Top” Width=”294″ />
<ListBox Height=”379″ HorizontalAlignment=”Right” Margin=”0,12,12,0″ Name=”listBox1″ VerticalAlignment=”Top” Width=”294″ ItemTemplate=”{DynamicResource DataTemplateItem}”/>
<Button Content=”Start” Height=”23″ HorizontalAlignment=”Left” Margin=”272,406,0,0″ Name=”Start” VerticalAlignment=”Top” Width=”75″ Click=”Start_Click” />
</Grid>
</Window>
MainWindow.Xaml.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;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Threading;
namespace AsyncLab
{
///<summary>
/// Interaction logic for MainWindow.xaml
///</summary>
publicpartialclassMainWindow : Window
{
privateCancellationTokenSource cancellationToken;
public MainWindow()
{
InitializeComponent();
}
private sync void Start_Click(object sender,RoutedEventArgs e)
{
ObservableCollection<string> uris = newObservableCollection<string>();
this.Start.IsEnabled = false;
try
{
var response = awaitnewHttpClient().GetAsync(http://msdn.microsoft.com);
string result = response.EnsureSuccessStatusCode().Content.ReadAsString();
this.textBox1.Text = result;
awaitTask.Run(() =>
{
MatchCollection mc = Regex.Matches(result, “href\\s*=\\s*(?:\”(?<1>http://%5B^\”]*)\”)”, RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
uris.Add(m.Groups[1].Value);
}
});
this.listBox1.ItemsSource = awaitTask.WhenAll(from uri in uris select DownloadItemAsync(newUri(uri)));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
this.Start.IsEnabled = true;
}
private static asyncTask<LinkInfo> DownloadItemAsync(Uri itemUri)
{
string item;
try
{
HttpClient httpClient = newHttpClient();
httpClient.MaxResponseContentBufferSize = 1000000;
var response = await httpClient.GetAsync(itemUri);
item = response.EnsureSuccessStatusCode().Content.ReadAsString();
}
catch
{
item = string.Empty;
}
LinkInfo linkInfo = newLinkInfo { Length = item.Length, Title = GetTitle(item), Html = item };
return linkInfo;
}
private static string GetTitle(string html)
{
if (html.Length == 0)
{
return“Not Found”;
}
Match m = Regex.Match(html, @”(?<=<title.*>)([\s\S]*)(?=</title>)”, RegexOptions.IgnoreCase);
return m.Value;
}
}
}
- HTML5 & CSS 3 has Rich Support in VS 2011 developer Preview in .NET Framework 4.5 Client Profile .
- The future of Web is Web11 with VS 2011 , HTML 5 & CSS 3.
- Javascript has more indent set of API in .NET Framework 4.5 Client Profile in VS 2011 developer Preview.
If you check WPF 4.5 development environment compared to WPF 4.0 environment , there has been addition of app.config file in VS 2011 developer preview WPF 4.5 development continues richer set of config support with SKU building. (Traditional Winforms Support)
Next article will describe more about HTML 5 & CSS 3…
You must be logged in to post a comment.