Bonjour,
Je vais essayer d'être le plus clair dans mes explications.
Ce que je cherche :
- Mettre un traitement de code long qui actuellement dans un timer, dans un background worker cadencée par le timer.
- J'ai déjà fait plusieurs recherche et essai et j'ai toujours le même défaut.
Défaut :
Dans le bgw :
=> Plantage avec message
Code : Sélectionner tout - Visualiser dans une fenêtre à part Me.TabPage3.BackColor = Grap_fond' Opération inter-threads non valide : le contrôle 'TabPage3' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé=> Plantage avec message
Code : Sélectionner tout - Visualiser dans une fenêtre à part Me.TabPage3.Controls.Add(Chart2)' Opération inter-threads non valide : le contrôle 'TabPage3' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.
Bien que j'ai fait mes diverses recherche sur un problème de Thread, je ne comprends pas.
Questions :
QU'elle est vraiment ce défaut car je le comprends pas vraiment? => Pour moi le pb vient du fait que je souhaite faire un changement d'état d'un objet!
Comment puis-je corrigé et arriver a faire ce traitement en fond de tache.
Merci d'avance.
Cordialement.
Voici le code total :
Timer :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick BackgroundWorker1.WorkerReportsProgress = True BackgroundWorker1.WorkerSupportsCancellation = True If BackgroundWorker1.IsBusy <> True Then BackgroundWorker1.RunWorkerAsync() End If End Sub
bgw :
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 'Créer un Chart Dim Chart2 As New Chart ' Créer ChartArea (zone graphique) Dim ChartArea1 As New ChartArea() ' Ajouter le Chart Area à la Collection ChartAreas du Chart Chart2.ChartAreas.Add(ChartArea1) type_fond_graph() type_axe_graph() type_axe_sec_graph() ' Définition du fond Chart2.BackColor = Grap_fond ChartArea1.BackColor = Grap_fond Me.TabPage3.BackColor = Grap_fond ' Opération inter-threads non valide : le contrôle 'TabPage3' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé '-------------------------------------------------------------------------------------- ' Coloration Axe X Principale ChartArea1.AxisX.LineColor = Grap_axe ChartArea1.AxisX.MajorGrid.LineColor = Grap_axe ChartArea1.AxisX.LabelStyle.ForeColor = Grap_axe ChartArea1.AxisX.MajorTickMark.LineColor = Grap_axe ' Coloration Axe Y Principale ChartArea1.AxisY.LineColor = Grap_axe ChartArea1.AxisY.MajorGrid.LineColor = Grap_axe ChartArea1.AxisY.LabelStyle.ForeColor = Grap_axe ChartArea1.AxisY.MajorTickMark.LineColor = Grap_axe If Echelle_Manuel.Checked = True Then ' Définition de la configuration des axes. ' Tracer Axe X Principale ChartArea1.AxisX.Minimum = Grap_Axe_X_Min ChartArea1.AxisX.Maximum = Grap_Axe_X_Max ChartArea1.AxisX.Interval = Grap_Axe_X_Interval_Label ' affichage Label de l'axe X tout les N ChartArea1.AxisX.MajorGrid.Interval = Grap_Axe_X_Interval_Label ' Pas du quadrillage -> 1 Division ChartArea1.AxisX.MajorTickMark.Interval = Grap_Axe_X_Interval_Label / 2 ' Pas de l'axe -> 1/2 Divison ' Tracer Axe Y Principale ChartArea1.AxisY.Minimum = Grap_Axe_Y_Min ChartArea1.AxisY.Maximum = Grap_Axe_Y_Max ChartArea1.AxisY.Interval = Grap_Axe_Y_Interval_Label ' Label de l'axe Y ChartArea1.AxisY.MajorGrid.Interval = Grap_Axe_Y_Interval_Label ' Pas du quadrillage ChartArea1.AxisY.MajorTickMark.Interval = Grap_Axe_Y_Interval_Label / 2 ' Pas de l'axe ChartArea1.AxisY.Interval = Grap_Axe_Y_Interval_Label ' Label de l'axe Y End If '-------------------------------------------------------------------------------------- If Axe_Secondaire.Checked = True Then ' Axe X Secondaire ' Affichage de l'axe secondaire ChartArea1.AxisX2.Enabled = AxisEnabled.True 'Suppression des label de l'axe secondaire ChartArea1.AxisX2.LabelStyle.Enabled = False ' Coloration ChartArea1.AxisX2.LineColor = Grap_axe_sec ChartArea1.AxisX2.MajorGrid.LineColor = Grap_axe_sec ChartArea1.AxisX2.LabelStyle.ForeColor = Grap_axe_sec ChartArea1.AxisX2.MajorTickMark.LineColor = Grap_axe_sec ' Tracer ChartArea1.AxisX2.Minimum = Grap_Axe_X_Min ChartArea1.AxisX2.Maximum = Grap_Axe_X_Max ChartArea1.AxisX2.Interval = Grap_Axe_X_Interval_Label / 2 ' Axe Y Secondaire ' Affichage de l'axe secondaire ChartArea1.AxisY2.Enabled = AxisEnabled.True 'Suppression des label de l'axe secondaire ChartArea1.AxisY2.LabelStyle.Enabled = False ' Coloration ChartArea1.AxisY2.LineColor = Grap_axe_sec ChartArea1.AxisY2.MajorGrid.LineColor = Grap_axe_sec ChartArea1.AxisY2.LabelStyle.ForeColor = Grap_axe_sec ChartArea1.AxisY2.MajorTickMark.LineColor = Grap_axe_sec ' Tracer ChartArea1.AxisY2.Minimum = Grap_Axe_Y_Min ChartArea1.AxisY2.Maximum = Grap_Axe_Y_Max ChartArea1.AxisY2.MajorGrid.Interval = Grap_Axe_Y_Interval_Label / 2 End If '-------------------------------------------------------------------------------------- ' Initialisation de la variable i = 1 L = 0 Chart2.Series.Clear() 'Création du nombre de serie en fonction de Nombre de voie selectionnées Do While (L <= Nbr_control And i <= Nbr_ES) Dim lp = Grap_Nom_InOut If Grap_Nom_InOut(i) <> Nothing And i <= Nbr_ES Then 'Chart2.Series.Add(i).Name = Grap_Sel_CB(i) Then ' Else Chart2.Series.Add(L).Name = Grap_Nom_InOut(i) & " - " & Grap_Unit(i) L = L + 1 End If i = i + 1 Loop Dim legend As New Legend 'On l'ajoute à la collection Legends n du Chart Chart2.Legends.Add(legend) Chart2.Size = New System.Drawing.Size(50, 50) ' legend.BackColor = Color.Transparent 'Suppression du graphique Suppression_graph() ' Gestion du type de graphique type_graph() ' Initialisation de la variable i = 0 J = 0 L = 0 'Replacement des indexation de la variable color Do While (L <= Grap_Color.Length - 1) Grap_Color_Memorie(L) = Grap_Color(L) If (Grap_Color(L) <> Nothing) Then Grap_Color_Resize(i) = Grap_Color(L) i = i + 1 End If L = L + 1 Loop ' Grap_Color = Grap_Color_Resize i = 0 L = 0 Do While (i < k) If J > Nbr_control Then J = Nbr_control End If If (J < Nbr_control And Nbr_control > 0) Then Dim lk = Chart2.Series ' Définition du type de graphique Chart2.Series(J).ChartType = Grap_Type ' Définition des points par séries Chart2.Series(J).Points.AddXY(i, multi(J, i)) ' Définition de la zone d'affichage Chart2.Series(J).ChartArea = "ChartArea1" ' Custom du graphique - Epaisseur des courbes Chart2.Series(J).BorderWidth = 1 ' Custom du graphique - Couleur des courbes Chart2.Series(J).Color = Grap_Color_Resize(J) 'Grap_Color(J) 'Affichage des marqueurs numérique If Marqueur_Numerique.Checked = True Then Chart2.Series(J).LabelForeColor = Grap_Color_Resize(J) Chart2.Series(J).IsValueShownAsLabel = True End If 'Affichage des marqueurs If Marqueur_Box.Checked = True Then Chart2.Series(J).MarkerColor = Grap_Color_Resize(J) 'Color.Cyan Chart2.Series(J).MarkerSize = 5 Chart2.Series(J).MarkerStyle = MarkerStyle.Circle End If ' If (Grap_Color(L) <> Nothing) Then ' If (J <> Nbr_control And Nbr_control > 0) Then J = J + 1 'End If ' End If ' If Grap_Nbpts_mesure = (Grap_Compteur_Nbpts) And Grap_Type_mesure = "Point" Then 'Timer1.Stop() Else J = 0 ' End If ' Grap_Compteur_Nbpts = Grap_Compteur_Nbpts + 1 End If L = L + 1 i = i + 1 Loop ' Initialisation de la variable i = 0 'M = M + 1 'Définition de la position du graphique Chart2.Location = New System.Drawing.Point(0, 0) ' Dimension du graphique Chart2.Size = New System.Drawing.Size(Me.TabPage3.Width - 10, Me.TabPage3.Height - 25) ' Ajouter le chart à la form Me.TabPage3.Controls.Add(Chart2) ' Opération inter-threads non valide : le contrôle 'TabPage3' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé. ' If TabControl1.SelectedIndex = 2 Then ' If Grap_duree_mesure = (Grap_Compteur_Temps / 1000) And Grap_Type_mesure = "Durée" Then ' Timer1.Stop() ' End If ' Grap_Compteur_Temps = Grap_Compteur_Temps + Timer1.Interval ' End If Chart = Chart2 ' ControlBmpToFile(Chart, "tet") End Sub
Partager