IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

vs2010 gel de l'IHM (form1)


Sujet :

C#

  1. #1
    Membre régulier
    Inscrit en
    Février 2003
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Février 2003
    Messages : 171
    Points : 77
    Points
    77
    Par défaut vs2010 gel de l'IHM (form1)
    Bonjour à tous,

    Comment fait-on pour rendre la form toujours active lors d'un traitement long ?

    Actuellement, si la methode de traitement est lancée, je ne peux plus appuyer sur tout autre bouton.

    Je pensais que si je mets une pause cela marcherai
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    System.Threading.Thread.Sleep(100);
    Mais ce n'est pas le cas...

    Qqu a une idée ?
    Merci pour votre aide

  2. #2
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Salut!!!

    Ta form est bloquée simplement parce que tu travail sur le thread Principal de ton application. Si tu utilises Sleep() cela bloquera aussi le main Thread.
    Une solution serait d’exécuter ta méthode longue dans un autre thread, ce qui laisserai à ta Form le main Thread (évitant ainsi le Freeze de ton appli ).

  3. #3
    Membre confirmé Avatar de jacky01
    Profil pro
    Développeur .NET
    Inscrit en
    Juin 2007
    Messages
    537
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Juin 2007
    Messages : 537
    Points : 527
    Points
    527
    Par défaut
    En effet FrameBreak à raison.

    Voici un exemple que j'ai fait :

    Un form d'attente avec une progressbar juste pour que le client se rende comtpe que l'appli n'a pas planté :

    Designer :
    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
     
    namespace MyNameSpace
    {
        partial class wait
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Windows Form Designer generated code
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.progress_info = new System.Windows.Forms.ProgressBar();
                this.label1 = new System.Windows.Forms.Label();
                this.thread_progress = new System.ComponentModel.BackgroundWorker();
                this.SuspendLayout();
                // 
                // progress_info
                // 
                this.progress_info.Location = new System.Drawing.Point(12, 12);
                this.progress_info.Name = "progress_info";
                this.progress_info.Size = new System.Drawing.Size(283, 23);
                this.progress_info.TabIndex = 0;
                this.progress_info.UseWaitCursor = true;
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(59, 47);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(198, 13);
                this.label1.TabIndex = 1;
                this.label1.Text = "Traitement en cours, veuillez patienter ...";
                this.label1.UseWaitCursor = true;
                // 
                // thread_progress
                // 
                this.thread_progress.DoWork += new System.ComponentModel.DoWorkEventHandler(this.thread_progress_DoWork);
                // 
                // wait
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(315, 78);
                this.ControlBox = false;
                this.Controls.Add(this.label1);
                this.Controls.Add(this.progress_info);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "wait";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                this.Text = "Traitement";
                this.UseWaitCursor = true;
                this.ResumeLayout(false);
                this.PerformLayout();
     
            }
     
            #endregion
     
            private System.Windows.Forms.ProgressBar progress_info;
            private System.Windows.Forms.Label label1;
            private System.ComponentModel.BackgroundWorker thread_progress;
        }
    }
    .cs
    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
     
    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 MyNameSpace
    {
        internal delegate void SetProgressBarDelegateHandler(ProgressBar pgb, int value);
        internal delegate void IncreaseProgressBarDelegateHandler(ProgressBar pgb, int value);
     
        public partial class wait : Form
        {
            private SetProgressBarDelegateHandler SetProgressBarDelegate;
            private IncreaseProgressBarDelegateHandler IncreaseProgressBarDelegate;
     
     
     
            public wait()
            {
                InitializeComponent();
     
                this.SetProgressBarDelegate = new SetProgressBarDelegateHandler(SetProgressBar);
                this.IncreaseProgressBarDelegate = new IncreaseProgressBarDelegateHandler(IncreaseProgressBar);            
     
                thread_progress.RunWorkerAsync();
            }
     
            /// <summary>
            /// Méthode qui augmente la valeur de la ProgressBar
            /// </summary>
            /// <param name="pgb">ProgressBar pour laquelle il faut augmenter la valeur</param>
            /// <param name="value">Valeur à ajouter</param>
            private void IncreaseProgressBar(ProgressBar pgb, int value)
            {
                if (pgb.Value == 100)
                    pgb.Value = 0;
     
                pgb.Increment(value);
            }
     
            /// <summary>
            /// Méthode qui assigne la valeur Maximale d'une ProgressBar
            /// </summary>
            /// <param name="pgb">ProgressBar pour laquelle il faut assigner la valeur max</param>
            /// <param name="value">Valeur max de la ProgressBar</param>
            private void SetProgressBar(ProgressBar pgb, int value)
            {
                pgb.Maximum = value;
                pgb.Value = 0;
            }
     
            private void thread_progress_DoWork(object sender, DoWorkEventArgs e)
            {           
                    Object pgb = (Object)progress_info;
                    Object val = (Object)10;
                    Thread.Sleep(250);
                    while (true)
                    {
                        Thread.Sleep(250);
     
                         try
                         {
                            this.Invoke(IncreaseProgressBarDelegate, pgb, val);
                         }
                         catch (Exception ex) { }
                    }
     
            }
        }
    }
    Dans ton Main :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    private void bt_genere_Click(object sender, EventArgs e)
            {
                wait win_wait = new wait();
                thread_info.RunWorkerAsync(win_wait);            
                win_wait.ShowDialog();
            }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private void thread_info_DoWork(object sender, DoWorkEventArgs e)
            {
                int a = 0;
                while (a <= 1)
                {
                    a++;               
                    Thread.Sleep(1000);
                }
     
                this.Invoke(CloseFormDelegate, e.Argument);            
            }
    Voila, petite subtilité, le thread ferme la fameuse form "wait" une fois le traitement terminé.

    N'hésite pas si tu as des questions .

    En espérant que ça t'aide
    Je fais ce que je dis et je dis ce que je veux .

Discussions similaires

  1. probleme d'affichage IHM 'Parametre incorrect'
    Par GENERYS dans le forum C++Builder
    Réponses: 5
    Dernier message: 26/11/2004, 16h53
  2. Développer une IHM en C++
    Par nxpyb dans le forum Bibliothèques
    Réponses: 2
    Dernier message: 17/11/2004, 16h10
  3. <<< IHM MFC: OPACITé >>>
    Par nico___23 dans le forum MFC
    Réponses: 8
    Dernier message: 10/11/2004, 00h02
  4. [SWT]mise a jour ihm SWT par un thread
    Par will82 dans le forum SWT/JFace
    Réponses: 1
    Dernier message: 06/08/2004, 11h37
  5. [IHM] Enchainement des écrans
    Par CanardJM dans le forum Composants VCL
    Réponses: 6
    Dernier message: 22/06/2004, 16h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo