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

Windows Forms Discussion :

[C# 2]Affichage d'une fenêtre d'attente


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut [C# 2]Affichage d'une fenêtre d'attente
    Je suis en train de développer une application réseau, je souhaite que lors du parcours du réseau qui est souvent long une fenêtre s'affiche du style "veuillez patienter" j'ai donc créer une winform que j'appelle comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
            private void frmMain_Load(object sender, EventArgs e)
            {
                frmWait waitingForm = new frmWait();
                waitingForm.Show();
                waitingForm.Refresh();
                // Traitements
                waitingForm.Close();
                waitingForm.Dispose();
    Lorsque j'exécute ce code ma fenêtre s'affiche le temps du traitement mais sans m'afficher les contrôles figurant sur cette winform, soit deux labels (veuillez patienter etc...)

    Pourriez vous m'aider SVP

    Je travaille sous VS .NET 2005

    Merci par avance

  2. #2
    Membre expérimenté Avatar de bossun
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 359
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Suisse

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 359
    Points : 1 443
    Points
    1 443
    Par défaut
    regarde au niveau du backgroundworker..... tu dois exécuter des opérations dans des threads différents...
    il vaut mieux prendre son pied que de se prendre la tête!!

    http://bossun.noxblog.com

  3. #3
    Membre confirmé

    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    481
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2006
    Messages : 481
    Points : 616
    Points
    616
    Par défaut
    Tu as pas enlever (par erreur) le INITComponant de la frmWait ?
    Passes tu bien dans le code de génération des labels ?

    Sinon fait l'ouverture de la form dans une autre Thread
    Je pense volontiers à penser aux choses auxquelles je pense que les autres ne penseront pas

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    J'ai suivi vos conseils et vous en remercie voici le code que j'ai écrit :

    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
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }
     
            private void frmMain_Load(object sender, EventArgs e)
            {
                bkWorker.RunWorkerAsync();
                frmWait waitingForm = new frmWait();
                waitingForm.Show();
     
                while (bkWorker.IsBusy)
                {
                    Application.DoEvents();
                }
     
     
                waitingForm.Close();
                waitingForm.Dispose();
     
            }
     
            private void btnConnect_Click(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
                btnConnect.Enabled = false;
     
                cmbDatabases.Items.Clear();
     
                Server server = new Server(cmbServers.Text);
     
                server.ConnectionContext.LoginSecure = false;
                server.ConnectionContext.Login = txtUser.Text;
                server.ConnectionContext.Password = txtPassword.Text;
     
                FillComboDatabases(server);
                btnConnect.Enabled = true;
                this.Cursor = Cursors.Default;
            }
     
     
     
            private void btnWinConnect_Click(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
                btnConnect.Enabled = false;
     
                cmbDatabases.Items.Clear();
     
                Server server = new Server(cmbServers.Text);
                server.ConnectionContext.LoginSecure = true;
     
                FillComboDatabases(server);
     
                btnConnect.Enabled = true;
                this.Cursor = Cursors.Default;
            }
     
            private void FillComboDatabases(Server server)
            {
                Int32 iCount = 0;
                foreach (Database db in server.Databases)
                {
                    iCount++;
                    cmbDatabases.Items.Add(db.Name);
                }
            }
     
            private void bkWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                //TODO : true local et false réseau
                DataTable dtSQLServer = SmoApplication.EnumAvailableSqlServers(true);
     
                foreach (DataRow drServer in dtSQLServer.Rows)
                {
                    string sServer = drServer["Server"].ToString();
                    if (drServer["Instance"] != null && drServer["Instance"].ToString().Length > 0)
                        sServer += @"\" + drServer["Instance"].ToString();
                    if (cmbServers.Items.IndexOf(sServer) < 0)
                        cmbServers.Items.Add(sServer);
                }
     
                Server local = new Server();
                string serverName = local.Name;
                if (local.InstanceName != null && local.InstanceName.Length > 0)
                    serverName += @"\" + local.InstanceName;
                Int32 idx = cmbServers.FindStringExact(serverName);
                if (idx >= 0)
                    cmbServers.SelectedIndex = idx;
            }
     
            private void bkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message.ToString());
                }
                else
                {
                    MessageBox.Show("OK");
                }
            }
     
        }
    Mais dans ce cas j'obtiens un message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Opération inter-threads non valide : le contrôle 'cmbServers' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.
    J'ai donc créé un truc plus simple :

    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
     
            private void button1_Click(object sender, EventArgs e)
            {
                backgroundWorker1.RunWorkerAsync();
                Form2 frm = new Form2();
                frm.Show();
     
                while (backgroundWorker1.IsBusy)
                {
                    Application.DoEvents();
                }
                frm.Close();
                frm.Dispose();
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                System.Threading.Thread.Sleep(2000);
            }
    Dans ce cas ca fonctionne, je ne comprends pas ?

    Merci encore de votre aide

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    je viens de lire ceci : http://glarde.developpez.com/dotnet/bgworker/ mais je ne vois pas comment l'adapter à mon code, quelqu'un aurait-il une piste SVP ?

    Merci

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    je viens encore de réécrire mon code qui est devenu :

    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
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
     
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
                //Control.CheckForIllegalCrossThreadCalls = false;
            }
     
            private List<String> listServers = new List<string>();
     
            private void frmMain_Load(object sender, EventArgs e)
            {
                bkWorker.RunWorkerAsync();
                frmWait waitingForm = new frmWait();
                waitingForm.Show();
     
                while (bkWorker.IsBusy)
                {
                    Application.DoEvents();
                }
     
     
                waitingForm.Close();
                waitingForm.Dispose();
     
                foreach (string serv in listServers)
                {
                    if (cmbServers.Items.IndexOf(serv) < 0)
                        cmbServers.Items.Add(serv);
                }
     
                Server local = new Server();
                string serverName = local.Name;
                if (local.InstanceName != null && local.InstanceName.Length > 0)
                    serverName += @"\" + local.InstanceName;
                Int32 idx = cmbServers.FindStringExact(serverName);
                if (idx >= 0)
                    cmbServers.SelectedIndex = idx; 
     
            }
     
            private void btnConnect_Click(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
                btnConnect.Enabled = false;
     
                cmbDatabases.Items.Clear();
     
                Server server = new Server(cmbServers.Text);
     
                server.ConnectionContext.LoginSecure = false;
                server.ConnectionContext.Login = txtUser.Text;
                server.ConnectionContext.Password = txtPassword.Text;
     
                FillComboDatabases(server);
                btnConnect.Enabled = true;
                this.Cursor = Cursors.Default;
            }
     
     
     
            private void btnWinConnect_Click(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
                btnConnect.Enabled = false;
     
                cmbDatabases.Items.Clear();
     
                Server server = new Server(cmbServers.Text);
                server.ConnectionContext.LoginSecure = true;
     
                FillComboDatabases(server);
     
                btnConnect.Enabled = true;
                this.Cursor = Cursors.Default;
            }
     
            private void FillComboDatabases(Server server)
            {
                Int32 iCount = 0;
                foreach (Database db in server.Databases)
                {
                    iCount++;
                    cmbDatabases.Items.Add(db.Name);
                }
            }
     
            private void bkWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                 //TODO : true local et false réseau
                DataTable dtSQLServer = SmoApplication.EnumAvailableSqlServers(false);
     
                listServers.Clear();
     
                foreach (DataRow drServer in dtSQLServer.Rows)
                {
                    string sServer = drServer["Server"].ToString();
                    if (drServer["Instance"] != null && drServer["Instance"].ToString().Length > 0)
                        sServer += @"\" + drServer["Instance"].ToString();
                   
                    listServers.Add(sServer);
                }
     
            }
     
     
            private void bkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message.ToString());
                }
                else
                {
                    toolStripStatusLabel1.Text = String.Format("Nombre de serveurs trouvés : {0}", listServers.Count.ToString());
                }
            }
     
            private void bkWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
     
            }
     
        }
    il n'y a plus d'erreur certes mais je ne le trouve pas élégant puis-je encore l'optimiser ?

    Merci

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Personne n'a une idée ?

Discussions similaires

  1. Affichage d'une fenêtre sans la barre d'outil
    Par itrione dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 20/06/2006, 23h43
  2. [C#] Problème d'affichage d'une fenêtre (event Load)
    Par PB-W dans le forum Windows Forms
    Réponses: 3
    Dernier message: 10/03/2006, 13h48
  3. pb affichage dans une fenêtre
    Par Mat 74 dans le forum Windows
    Réponses: 8
    Dernier message: 27/11/2005, 23h14
  4. Destruction automatique d'une fenêtre d'attente
    Par Depteam1 dans le forum MFC
    Réponses: 4
    Dernier message: 20/09/2005, 13h40

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