Bonjour,
Je me heurte à un problème de threading et je souhaiterais vous exposer ma problématique.
Lors d'un traitement lourd, je tente d'afficher dans un thread parallèle, une fenêtre d'attente dynamique. Voici ma classe :
Ma fenêtre s'affiche parfaitement et cela marche relativement bien.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; using System.Drawing; namespace Test.Tools { public class WaitDialogThread { Thread waitDialogThread; string textToDiplay; Point location; public WaitDialogThread(string text, Point loc) { textToDiplay = text; location = loc; } public void Start() { waitDialogThread = new Thread(new ThreadStart(DisplayWaitDialog)); waitDialogThread.SetApartmentState(ApartmentState.STA); waitDialogThread.Priority = ThreadPriority.Highest; waitDialogThread.Start(); } public void Stop(Form formToRefresh) { if (waitDialogThread != null) { waitDialogThread.Abort(); if (waitDialogThread.IsAlive) waitDialogThread.Join(); if (formToRefresh != null) { formToRefresh.Focus(); formToRefresh.Activate(); formToRefresh.Refresh(); formToRefresh.BringToFront(); } } } /// <summary> /// Display a wait dialog in a new thread for Excel export. /// </summary> public void DisplayWaitDialog() { Point locationToDisplay = new Point(location.X + 407, location.Y + 329); WaitDialog wd = new WaitDialog(textToDiplay, locationToDisplay); Application.EnableVisualStyles(); Application.Run(wd); } } }
Lorsque j'ai finis mon traitement, je fais un Abort() sur mon Thread, c'est très sale et dans certains cas précis, ca plante car mon application reste bloquée son mon Join() => Je fais le Join afin d'attendre que le Thread soit fermé.
Ce que je voudrais : Communiquer avec mon thread afin de lui dire de closer sa fenêtre proprement.
Avez-vous une idée?
Partager