ben oui .... il y a ben un gelegue de definit ....

l'acces a ce controle se fait dans la classe de la form ...

voici le code :

de la form :

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AppThread;

namespace TestThread
{
    public partial class FRM_Prog : Form
    {
        theThread_Scan aScan;
        
        public FRM_Prog()
        {
            InitializeComponent();
            aScan = new theThread_Scan();
        }

        private void FRM_Prog_Shown(object sender, EventArgs e)
        {
            aScan.OnTextChanged += new theThread_Scan.TextGeneratedEventHandler(aScan_OnTextChanged);
            aScan.Start();            
        }

        void aScan_OnTextChanged(object sender, GenerateTextEventArgs e)
        {
            label1.Text = e.EventText;
            throw new Exception("The method or operation is not implemented.");
        }

        private void FRM_Prog_Load(object sender, EventArgs e)
        {

        }
    }
}
et celui du thread :

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
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Collections;
using System.ComponentModel;
 
 
namespace AppThread
{
    public class GenerateTextEventArgs : EventArgs
	{
		private string myEventText = null;
 
        public GenerateTextEventArgs(string theEventText)
		{			
			if (theEventText == null) throw new NullReferenceException();
			myEventText = theEventText; 
		}
 
		public string EventText
		{
			get { return this.myEventText; }
		}	
	}
 
    class theThread_Scan
    {
        private Thread ThreadScan;
 
        public delegate void TextGeneratedEventHandler(object sender, GenerateTextEventArgs e);
        public event TextGeneratedEventHandler OnTextChanged;
 
        public theThread_Scan()
        {
            this.ThreadScan = new Thread(new ThreadStart(ThreadLoop));
            this.ThreadScan.Name = "TEST_THREAD";            
        }
 
        public void Start()
        {
            this.ThreadScan.Start();
        }
 
        public void Stop()
        {
            this.ThreadScan.Abort();
        }
 
        private void ThreadLoop()
        {
            for (int i = 0; i < 100; i++)
            {
                GenerateTextEventArgs e = new GenerateTextEventArgs("Compteur = " + i.ToString());
                if (e != null) OnTextChanged(this, e);
 
                Thread.Sleep(100);              
            }
        }
    }
}