Bonjour,

je m'y connais très peu en C# et j'y développe pour mon propre apprentissage un petit programme tout bête mettant en action des threads.

J'ai une picture box que je crée dans une classe et que j'aimerai faire rebondir dans un carré.
Cette classe est instanciée à l'aide d'un thread, le but étant que 3 balles correspondraient à 3 threads.

J'ai une erreur dans mon code, pouvez-vous m'aider svp?
Tout est dans le form1, pas de modules (pour le moment)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication2
{

    public partial class Form1 : Form
    {
        ball une_ball;
        ball une_ball2;

        ball param;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            une_ball = new ball(100, 50);
            une_ball2 = new ball(400, 400);
            this.Controls.Add(une_ball.maball);
            this.Controls.Add(une_ball2.maball);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            param = une_ball;
            Thread thr_lap1 = new Thread(new ThreadStart(param.LanceThread));
            thr_lap1.Start();

            //param = une_ball2;
            //Thread thr_lap2 = new Thread(new ThreadStart(LanceThread));
            //thr_lap2.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

    }

    public class ball
    {
        public PictureBox maball;
        public PictureBox getMaBalle() { return maball; }

        public ball(int x, int y)
        {
            maball = new System.Windows.Forms.PictureBox();
            maball.Image = global::WindowsApplication2.Properties.Resources.Hiver;
            maball.Location = new System.Drawing.Point(x, y);
            maball.Name = "lol";
            maball.Size = new System.Drawing.Size(10, 10);
            maball.TabIndex = 0;
            maball.TabStop = false;
        }


        public PictureBox pict1 = new System.Windows.Forms.PictureBox();
        public PictureBox getpict1() { return pict1; }


        public void LanceThread()
        {
            if (pict1.InvokeRequired)
                pict1.Invoke(new TextBoxInvokeHandler(Avancer));
            else
                Avancer();
        }

        public delegate void TextBoxInvokeHandler();

        public void Avancer()
        {
            int x = maball.Location.X;
            int y = maball.Location.Y;
            //int x = this.pictureBox1.Location.X;
            //int y = this.pictureBox1.Location.Y;
            int dx = -1;
            int dy = -4;
            int vitesse = 5;

            Random Randomclass = new Random();

            for (; ; )
            {
                int param_x = x;
                param_x += dx * vitesse;

                if ((param_x > 490) || (param_x < 100))
                    dx = -dx;

                int param_y = y;
                param_y += dy * vitesse;

                if ((param_y > 440) || (param_y < 50))
                    dy = -dy;

                x += dx * vitesse;
                y += dy * vitesse;

                maball.Location = new System.Drawing.Point(x, y);
                Thread.Sleep(50);
            }

            //this.pictureBox1.Location = new System.Drawing.Point(100, 50);
            //this.pictureBox1.Location = new System.Drawing.Point(490, 440);

        }
    }
}
L'erreur se trouve ici puisqu'il me dit j'accede déjà à maball.location via un autre thread. Or j'ai un invoke pour éviter ceci...

Merci d'avance.