Bonjour, j'ai du mal à comprendre pourquoi ces deux codes, à priori semblables, ont un comportement différent.

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
using System;
using System.Threading;
 
namespace ConsoleApplication1
{
    class Program
    {
        static private void MaMethode()
        {
            int counter = 0;
            while (true)
            {
                Thread.Sleep(1000);
                counter++;
                Console.WriteLine(counter.ToString());
            }
        }
        static void Main(String[] args)
        {
            Thread MaTache = new Thread(new ThreadStart(MaMethode));
            MaTache.IsBackground = true;
            MaTache.Start();
            Thread.Sleep(2500);
 
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private void MaMethode()
        {
            int counter = 0;
            while (true)
            {
                Thread.Sleep(1000);
                counter++;
                label1.Text = counter.ToString();
            }
        }
        public Form1()
        {
            InitializeComponent();
            Thread MaTache = new Thread(new ThreadStart(MaMethode));
            MaTache.IsBackground = true;
            MaTache.Start();
            Thread.Sleep(2500);
        }
    }
}
Dans le premier cas la sortie Console est:
1
2

Dans le second cas le label du form qui affiche le compteur continue de s'incrémenter à l'infini.

Merci pour votre aide