Async et Await sur fonction perso
Bonjour tout le monde,
Je suis en train de me faire la main sur le développement Windows 8 en VB.NET et je bloque sur l'utilisation de l'asynchronisme...
Je m'explique :Je voudrais créer une fonction qui tourne en background sans immobiliser le thread de l'application.
Le Framework 4.5 a implémenté les mots clé Async et Await qui sont sencé simplifier tout ça. Mais j'arrives à les utiliser pour appeler leurs API http etc mais pas à créer ma propre fonction asynchrone.
Pour que vous puissiez m'aider, je vous livre ici mon code de test.
Page XAML :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <Page
x:Class="Test_Parallelisme.MainPage"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test_Parallelisme"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource HeaderTextStyle}" Text="Tests parallelisme" Margin="10" />
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal">
<Button x:Name="btn_Go" Click="btn_Go_Click_1" Grid.Column="1" Grid.Row="1" Content="Exécuter" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" />
<Button Grid.Column="1" Grid.Row="1" Content="Toujours actif ?" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" />
</StackPanel>
<TextBlock Grid.Row="2" Grid.Column="1" Margin="10" x:Name="tblk_Resultat" Text="En attente..."/>
</Grid>
</Page> |
Code behind VB :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Public NotInheritable Class MainPage
Inherits Page
''' <summary>
''' Invoked when this page is about to be displayed in a Frame.
''' </summary>
''' <param name="e">Event data that describes how this page was reached. The Parameter
''' property is typically used to configure the page.</param>
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
End Sub
Private Async Sub btn_Go_Click_1(sender As Object, e As RoutedEventArgs)
Me.btn_Go.IsEnabled = False
Dim bln_Resultat As Boolean = Nothing
'---------------------------------------------------------------------------
'Démarrer la tâche, continuer a faire des choses dans ce Thread et ensuite attendre le résultat pour continuer
'---------------------------------------------------------------------------
'
'Instancier et démarrer (c'est automatique) la tâche
Dim obj_Tache As Task(Of Boolean) = Me.tsk_CalculAsync
'Faire tout un tat de choses ici
Dim A As Integer = 0
Dim B As Integer = 50
A += B '50
A += B '100
A += B '150
'Attendre le résultat de la tâche et continuer ensuite
bln_Resultat = Await obj_Tache
A = B '50
'---------------------------------------------------------------------------
Me.btn_Go.IsEnabled = True
End Sub
Private Async Function tsk_CalculAsync() As Task(Of Boolean)
For int_Valeur As Integer = 0 To 1000000
Me.tblk_Resultat.Text = int_Valeur.ToString("0000000")
Next
Return True
End Function
End Class |
Quand je lance mon code, le deuxième bouton ne répond plus... Et en plus, je ne vois pas l'incrémentation de ma valeur dans mon Textblock...
Help !!