Bonjour,
J’ai trouvé sur le net un tuto c# sur les threads, mais il ne semble pas fonctionner. Il compile, mais au lancement, il y a une erreur qui dit que mon thread essaye de modifier un objet qui n'a pas été créée dans ce thread (c'est pourtant bien ce que je souhaite faire).
L'url du tuto : http://csharpro.developpez.com/page9.php
Quelqu'un voit-il un problème dans ce code?
Dois-je utiliser des délégués?
Merci beaucoup,
David.
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { private int ti, tj; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Thread t1, t2; ti = 0; tj = 0; t1 = new Thread(new ThreadStart(implemente_ti)); t1.Start(); t2 = new Thread(new ThreadStart(implemente_tj)); t2.Start(); } private void implemente_ti() { ti++; this.textBox2.AppendText("\r\n" + ti.ToString()); if (ti <= 10000) implemente_ti(); } private void implemente_tj() { tj++; textBox3.AppendText("\r\n" + tj.ToString()); if (tj <= 10000) implemente_tj(); } } }
Partager