Bonjour
J'ai toujours pas bien digéré l'usage des Thread
Dans l'exemple ci-apres depuis une form j'appelle une methode dans un nouveau thread en utilisant Treadpool
Ma methode contient deux event permettant de mettre eu datagridview a jour dans la form
En debug, ca marche tres bien.
En release ca bloque en cours de traitement et je dois fermer l'application
Je sais en tout cas que je n'arrive pas au bout de la boucle de traitement
Un oeil de lynx repere-t-il une grosse betise ?
Voici le code d'appel
Voici la synthese du traitement
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 .. DistMatrix dm = new DistMatrix(); dm.VV = VV; dm.AddRow += new DistMatrix.StepRowDelegateHandler(Dist_StepRow); dm.AddCell += new DistMatrix.StepCellDelegateHandler(Dist_StepCell); ThreadPool.QueueUserWorkItem(new WaitCallback(Process),dm); } public delegate void StepCellDelegateHandler(int a, int b); public delegate void StepRowDelegateHandler(DataTable dt); // ***************************************************************************************** void Process(Object DM) { DataTable dtn = ((DistMatrix)DM).getMatrix(dtAdr); } // ***************************************************************************************** private void Dist_StepCell(int a, int b) { if (this.InvokeRequired) { try { this.Invoke(new StepCellDelegateHandler(StepCell), new object[] { a, b }); return; } catch (Exception e) { MessageBox.Show(e.Message); } } else { this.StepCell(a, b); } }
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 // *********************************************************************************** public DataTable getMatrix(DataTable dtIn) { DataTable dtMatrix=CreateTable(dtIn); for (i = 0; i < dtIn.Rows.Count; i++) { DataRow cRowY = dtIn.Rows[i]; string fromY = cRowY["addrId"].ToString(); for (j = 0; j < dtIn.Rows.Count; j++) { DataRow cRowX = dtIn.Rows[j]; addDtCell(dtMatrix,cRowX["addrId"].ToString(),fromY,5); if (AddCell != null) { AddCell(j,i); } } } if (AddRow != null) { AddRow(dtMatrix); } if (AddCell != null) { AddCell(-1, -1); } return dtMatrix; } // *********************************************************************************** private void addDtCell(DataTable DstMatrix, string rX, string rY, double dist) { DataView dv = new DataView(DstMatrix); dv.Sort = "Start"; int idx = dv.Find(rY); DataRow cRow = null; if (idx < 0) { cRow = DstMatrix.Rows.Add(); cRow["Start"] = rY; if (AddRow != null) { AddRow(DstMatrix); } } else { cRow = dv[idx].Row; } if (dist > 0.01) { cRow[rX] = dist; } }
Partager