Thread: Lancer un processus en tâche de fond(Background)

Author:


{filelink=17382}

using System;
using System.Windows.Forms;
using System.Threading;

    public class ProcessusBackground : System.Windows.Forms.Form
    {
        /*
         *  Lancer la fenêtre en mode réduit
         *  Et Exécuter le Thread en Tâche de fond
         *  Afficher un Message à la fin du processus
         *
         * */
        private System.Windows.Forms.Button btnAnnul;

        public ProcessusBackground()
        {
            Text = "Exemple de processus en tâche de fond";
            btnAnnul = new Button ();
            SuspendLayout();
            btnAnnul.Text = "Annuler";
            btnAnnul.Name = "btnAnnul";
            btnAnnul.Size = new System.Drawing.Size (72, 30);
            btnAnnul.Location = new System.Drawing.Point ((ClientRectangle.Width - btnAnnul.Size.Width) / 2, ClientRectangle.Height - 35);
            Controls.AddRange(new System.Windows.Forms.Control[] {this.btnAnnul});
            btnAnnul.Click += new System.EventHandler(OnClickButton1);
            this.Closed += new System.EventHandler(OnClickButton1);
            ResumeLayout (false);
            this.Visible = false;
        }

        public void DelayVisible ()
        {
            while (true)
            {
                // Attendre 5 secondes avant de lancer la fenêtre
                Thread.Sleep (5000);
                DialogResult result = MessageBox.Show ("Fin d'exécution du processusnAfficher la Fenêtre?", "Affichage", MessageBoxButtons.YesNoCancel);
                switch (result)
                {
                    case DialogResult.Yes:
                        this.ShowDialog();
                        return;
                    case DialogResult.No:
                        continue;
                    case DialogResult.Cancel:
                        Application.Exit ();
                        break;
                }
                return;
            }
        }
        private void OnClickButton1(object sender, System.EventArgs e)
        {
            Application.Exit ();
        }

        [STAThread]
        static void Main()
        {
            ProcessusBackground form = new ProcessusBackground();
            Thread delay = new Thread (new ThreadStart(form.DelayVisible));
            delay.Start ();
            Application.Run();
        }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *