Mon application est bloquée, freeze

ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010 - 26 oct. 2010 à 21:02
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010 - 28 oct. 2010 à 20:13
Bonjour comment lancer l'attente d'un programme ou lancer un traitement assez long sans freezer mon application !

impossible lors du sleep de deplacer mon application, de la reduire etc....

merci d'avance

voila mon code simple, on click sur un bouton qui lance un sleep, ou bien une boucle de 1 à 1000000, impossible durant le traitement de deplacer la form

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;
 
namespace TestPause1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnENVOYER_Click(object sender, EventArgs e)
        {
            WaitTime(10);
        }
 
 
 
 
        private void WaitTime(int attendre)
        {
            while (0 < attendre)
            {
                System.Threading.Thread.Sleep(1000); // 1 secondes 1000
                attendre = attendre - 1;
                //lblATT.Text = SIMNLib.cFonctions.RightChaine(string.Format("  {0}  ", att), 5);
                //lblATT.Refresh();
 
            }
 
        }
    }
}
 


Eric
[www.elotch.com www.elotch.com]

11 réponses

ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
26 oct. 2010 à 21:44
On m'a conseille de lancer mon traitement dans un thread

voila ce que j'ai fait mais cela ne marche pas mon application est toujours gelé

qu'est ce que je ne fais pas correctement ???? merci de votre aide


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 TestThread2
{
    public partial class Form1 : Form
    {

        private Thread t;
        private Label labelTest;
        private delegate void ChangeLabel(string str);

        public Form1()
        {
            InitializeComponent();

            this.labelTest = new System.Windows.Forms.Label();
            this.labelTest.AutoSize = true;
            this.labelTest.Location = new System.Drawing.Point(29, 23);
            this.labelTest.Size = new System.Drawing.Size(106, 13);
            this.labelTest.Text = "Label avant opération";
            this.Controls.Add(this.labelTest);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            t = new Thread(new ThreadStart(ThreadMethod));
            t.Start();

        }

        private void ThreadMethod()
        {
            this.Invoke(new ChangeLabel(ChangeLabelText), "Label après opération");
            //les instuctions se trouvant ici seront appelées
            //une fois la méthode ChangeLabelText executée et terminée.
            MessageBox.Show("Invoke terminé");

        }

        private void ChangeLabelText(string str)
        {
            labelTest.Text = str;
            Thread.Sleep(5000);
        }




    }
}


Eric
[www.elotch.com www.elotch.com]
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
26 oct. 2010 à 22:55
Logique, le Thread.Sleep se trouve toujours dans thread principal (celui de l'écran, et non dans le nouveau thread créé.
Déplace le dans la méthode ThreadMethod, ça devrait aller mieux.


[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
0
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
26 oct. 2010 à 23:09
Oui effectivement en deplacant Thread.Sleep dans ThreadMethod
c'est ok
mais comment attendre la fin du thread dans le button1_Click
pour afficher le message que lorsque la thread est terminée ?



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 TestThread2
{
    public partial class Form1 : Form
    {

        private Thread t;
        private Label labelTest;
        private delegate void ChangeLabel(string str);

        public Form1()
        {
            InitializeComponent();

            this.labelTest = new System.Windows.Forms.Label();
            this.labelTest.AutoSize = true;
            this.labelTest.Location = new System.Drawing.Point(29, 23);
            this.labelTest.Size = new System.Drawing.Size(106, 13);
            this.labelTest.Text = "Label avant opération";
            this.Controls.Add(this.labelTest);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            t = new Thread(new ThreadStart(ThreadMethod));
            t.Start();
            MessageBox.Show("This is the main thread");  

        }


private void ThreadMethod()
        {
            Thread.Sleep(5000);
            this.Invoke(new ChangeLabel(ChangeLabelText), "Label après opération");
            //les instuctions se trouvant ici seront appelées
            //une fois la méthode ChangeLabelText executée et terminée.
            MessageBox.Show("Invoke terminé");

        }

        private void ChangeLabelText(string str)
        {
            labelTest.Text = str;
                   }


Eric
[www.elotch.com www.elotch.com]
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
26 oct. 2010 à 23:20
Le message, tu peux le mettre dans le thread à la fin, plutôt que dans le button_click. C'est d'ailleurs ce que tu fais avec le messagebox.


[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
26 oct. 2010 à 23:33
Oui mais le but est de lancer un traitement et mettre en pause

Voila en schema ==>

debut thread traitement principal
boucle 1
....
....
pause debut thread traitement 2 sleep (pause)
fin de thead traitement 2
suite de la boucle 1
fin de thread traitement principal


Eric
[www.elotch.com www.elotch.com]
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
27 oct. 2010 à 08:22
Bonjour,

Le plus simple en Windows Forms est d'utiliser le contrôle BackgroundWorker qui fait ça tout seul !

Voici un exemple d'application qui l'utilise : [url]http://www.csharpfr.com/codes/BATCHIMAGECONVERTER_50220.aspx/url

Sébastien FERRAND
Lead Developpeur
Microsoft Visual C# MVP 2004 - 2009
Blog Photo
0
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
27 oct. 2010 à 21:44
Merci oui ca m'a bien aidé

seule difficulté est l'utilisation de invoke pour modifier des zones dans la form

Eric
[www.elotch.com www.elotch.com]
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
28 oct. 2010 à 05:45
Avec le BackgroundWorker, il n'y a pas besoin de Invoke.

Sébastien FERRAND
Lead Developpeur
Microsoft Visual C# MVP 2004 - 2009
Blog Photo
0
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
28 oct. 2010 à 08:09
impossible de modifier le text d'un textbox ou d'un label sans faire

this.Invoke(new ChangeLabel(ChangeTextMethod), lblATT, "");

comment fais tu sinon ??



Eric
[www.elotch.com www.elotch.com]
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
28 oct. 2010 à 08:42
il suffit de modifier ton text dans l'évènement ProgressChanged du BackgroundWorker... comme dans la source que je t'ai fourni.

Sébastien FERRAND
Lead Developpeur
Microsoft Visual C# MVP 2004 - 2009
Blog Photo
0
ericaix13 Messages postés 17 Date d'inscription vendredi 28 mars 2003 Statut Membre Dernière intervention 31 octobre 2010
28 oct. 2010 à 20:13
ok merci pour l'info


Eric
[www.elotch.com www.elotch.com]
0
Rejoignez-nous