Crossthreading - appel d'une méthode via un délégué synchrone

Contenu du snippet

Je poste ce code en réponse a cette question sur le forum :
http://www.csharpfr.com/forum.v2.aspx?ID=600719#3

Il démontre comment mettre en oeuvre une déléguation synchrone pour eviter le Cross-Threading.
Il s'agit donc d'un exemple en complément de ce tutorial : http://www.csharpfr.com/tutorial.aspx?ID=174

Source / Exemple :


using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class Form1 : Form
    {
        private Icon ico1 = new Icon("ico1.ico");
        private Icon ico2 = new Icon("ico2.ico");

        public Form1()
        {
            InitializeComponent();
            this.timer1.Interval = 1000;
            this.timer1.Start();
            this.timer1.Tick += new EventHandler(timer1_Tick);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Au tick du premier timer, on declenche un thread
            this.timer1.Stop();
            Thread t = new Thread(new ThreadStart(ThreadMethod));
            t.Start();
        }

        private void ThreadMethod()
        {
            // Et dans ce thread, on declenche la méthode qui fera clignoter l'icone de la form 
            // Via un appel synchrone à un délégué
            // Le null signifie que nous ne passons aucun parametre a la méthode (qui n'en attend pas)
            Invoke(new DoBlinkDelegate(DoBlink), null);
        }

        private System.Windows.Forms.Timer timerIcon = new System.Windows.Forms.Timer();
        private delegate void DoBlinkDelegate();
        private void DoBlink()
        {
            timerIcon.Interval = 1000;
            timerIcon.Tick+= new EventHandler(t_Tick);
            timerIcon.Start();
        }

        private bool ico;
        void t_Tick(object sender, EventArgs e)
        {
            // Lors du tick du timer, on fait varier l'icone
            ico = !ico ? true : false;
            if (ico) this.notifyIcon1.Icon = ico1;
            else this.notifyIcon1.Icon = ico2;
        }

        private NotifyIcon notifyIcon1;
        private System.Windows.Forms.Timer timer1;
        private System.ComponentModel.IContainer components = null;
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.SuspendLayout();
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.Text = "notifyIcon1";
            this.notifyIcon1.Visible = true;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
    }
}

Conclusion :


L'exemple est construit en fonction du besoin de la personne qui avait posé la question sur le forum, a savoir :
Un timer appele un thead tout les X temps. Ce thread doit verifier la presence d'eventuelles nouvelles données dans une base de données (chose qui n'est pas mise en oeuvre dans l'exemple car ce n'etait pas le but).
Si de nouvelles donneés sont trouvées (et on dira que c'est notre cas ;) ), alors le thread appelle une méthode qui fait clignoter l'icone d'un NotifyIcon (clignotement realisé grace a un second timer).

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.