Bonjour à tous,
Je viens de lire des tuto sur l’asynchronisme principalement pour l' E/S qui est mon cas d'utilisation.
j'ai 2 codes identiques avec un placement different de l'appel à la methode Async, les voici :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private void Application_Startup(object sender, StartupEventArgs e) { //Vue principale ViewWindow vw = new ViewWindow(); //Affichage du menu IMenuController mc = new MenuController(new MenuRepository()); MenuView mv = new MenuView(); MenuViewModel mvm = new MenuViewModel(mc, mv); mv.ShowInWindow(false, vw, "MiningManager", 800, 450, Dock.Top, mvm.OnWindowClosed); //mc.Start(); // Affichage Status IStatusController sc = new StatusController(new StatusRepository()); StatusView sv = new StatusView(); StatusViewModel svm = new StatusViewModel(sc, mv); sv.ShowInWindow(vw, Dock.Bottom, svm.OnWindowClosed); // Affichage du container IContainerController cc = new ContainerController(new ContainerRepository()); ContainerView cv = new ContainerView(); ContainerViewModel cvm = new ContainerViewModel(cc, cv); cv.ShowInWindow(vw, null, svm.OnWindowClosed); // Methode de test ddu DbContext Task t = TestContextAsync(); } private async Task TestContextAsync() { MiningContext ctx = new MiningContext(); await ctx.Communs.LoadAsync(); bool b = ctx.Communs.Any(x => x.Nom == "Finder F-101"); bool c = ctx.Communs.Any(x => x.Nom == "Finder F-101mjm"); }de ces 2 codes, seul le dernier avec l'appel asynchrone au départ ne gèle pas la construction de la fenêtre (que j'utilise Task ou void change rien)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private void Application_Startup(object sender, StartupEventArgs e) { // Methode de test ddu DbContext Task t = TestContextAsync(); //Vue principale ViewWindow vw = new ViewWindow(); //Affichage du menu IMenuController mc = new MenuController(new MenuRepository()); MenuView mv = new MenuView(); MenuViewModel mvm = new MenuViewModel(mc, mv); mv.ShowInWindow(false, vw, "MiningManager", 800, 450, Dock.Top, mvm.OnWindowClosed); //mc.Start(); // Affichage Status IStatusController sc = new StatusController(new StatusRepository()); StatusView sv = new StatusView(); StatusViewModel svm = new StatusViewModel(sc, mv); sv.ShowInWindow(vw, Dock.Bottom, svm.OnWindowClosed); // Affichage du container IContainerController cc = new ContainerController(new ContainerRepository()); ContainerView cv = new ContainerView(); ContainerViewModel cvm = new ContainerViewModel(cc, cv); cv.ShowInWindow(vw, null, svm.OnWindowClosed); } private async Task TestContextAsync() { MiningContext ctx = new MiningContext(); await ctx.Communs.LoadAsync(); bool b = ctx.Communs.Any(x => x.Nom == "Finder F-101"); bool c = ctx.Communs.Any(x => x.Nom == "Finder F-101mjm"); }
le premier fige la construction de la fenêtre jusque à ce que la méthode asynchrone termine.
j'ai pourtant compris que l'instruction await rend le contrôle à l'appelant le temps que la tache se termine, mais visiblement la, ça ne fonctionne pas.
Ai-je oublié une étape? ou faut il utiliser la méthode processeur avec Task.Run?
Merci pour vos éclaircissements
Partager