In Windows 8, the "Asynchronous" feature of C #5.0 can be used, with simple and elegant code, greatly reducing the complexity of asynchronous programming. In Windows phone7, async CTP can also be used for implementation, the procedure is as follows.
First download async CTP http://www.microsoft.com/en-us/download/details.aspx? Id = 9983
The installation of the necessary conditions is vs2010 SP1 not install SP1 friends please download: http://download.microsoft.com/download/E/ B /A/EBA0A152-F426-47E6-9E3F-EFB686E3CA20/VS2010SP1dvd1.iso
Do not rush to install async CTP after downloading. Because the installation program conflicts with many vs patches and cannot be used even if it is installed, we must first exclude these mines.
Bytes ---------------------------------------------------------------------------------------------------------------
If it is a Windows 8 system, you must install async CTP before Silverlight 5 is installed; otherwise, it cannot be used. If Silverlight 5 is installed, uninstall it first. If vs2012 is installed, we recommend that you uninstall it first.
You can delete the following patches on the control panel: kb2635973, kb2615527, and kb4105410.
(Specific can refer to: http://blogs.msdn.com/ B /lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx)
After the installation is complete, the file asyncctplibrary_phone.dll appears in the Microsoft Visual Studio async CTP folder in my documents.
In this case, you can create a Windows Phone program to reference the DLL asyncctplibrary_phone.dll.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading.Tasks;
namespace CRSAsyncCTP
{
public partial class MainPage: PhoneApplicationPage
{
// Constructor
public MainPage ()
{
InitializeComponent ();
}
private int fun (object num)
{
int i = (int) num;
if (i == 0 || i == 1) return 1;
return i * fun (i-1);
}
private async Task <int> FFun (int num)
{
return await Task <int> .Factory.StartNew (new Func <object, int> (fun), num);
}
private async void button1_Click (object sender, RoutedEventArgs e)
{
string str = textBox1.Text;
int number = int.Parse (str);
var result = await FFun (number);
base.Dispatcher.BeginInvoke (() =>
{
MessageBox.Show (result.ToString ());
});
}
}
}
I made a demo: crsasyncctp.rar
Now, let's take a look at it.