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 :

Problème de Timer qui ne marche qu'une foi


Sujet :

C#

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2011
    Messages : 63
    Points : 47
    Points
    47
    Par défaut Problème de Timer qui ne marche qu'une foi
    Bonjour.
    J'ai une checkbox qui une fois coché définie un interval d'une seconde sur mon timer et le passe à true.
    La première fois que je coche ma checkbox le timer_Tick se lance et fais tourner un backgroundworker qui redéfinis entre autre le timer.interval
    En mode débug j'ai bien mon interval qui change et mon timer à true
    Mais à la fin de mon nouvel interval le timer_Tick ne se relance pas ?????
    Pareil si je décoche (timer = disable) et recoche ma checkbox qui redéfinie bien mon interval à 1 seconde et mon timer à true le timer_Tick ne veux plus se lancer.

    Alors j'ai envie de dire : Why the Fuck ?

    j'ai une bonne dizaine de timer dans mon programme qui sont tous programmé pareil, s'active sur checkbox lance un timer_tick qui lui même lance un backgroundworker

    La seule différence avec les autres c'est que celui la redéfinis l'interval dans le backgroundworker mais je vois pas pourquoi ça le coincerai surtout qu'il me sort aucune erreur.

  2. #2
    Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2011
    Messages : 63
    Points : 47
    Points
    47
    Par défaut
    Grosso merdo le code
    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
     
        public class Form1 : Form
        {
            private int crafttimer;
            private BackgroundWorker bgwcraft;
            private System.Windows.Forms.Timer timercraft;
     
            private void bgwcraft_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    this.timercraft.Enabled = false;
                    this.crafttimer = 0;
                    .
                    .
                    .
                    //Du code qui redéfinie mon crafttimer
                    .
                    .
                    .
                    if (this.crafttimer > 0)
                    {
                        UIUpdaterDelegate delegate1 = delegate
                        {
                            this.label191.Text = "tps : " + Convert.ToString(this.crafttimer);
                        };
                        this.label191.Invoke(delegate1);
                        this.timercraft.Interval = this.crafttimer + 5000; // Je rajoute 5s de marge pour être sur que ce soit bien finis
                        this.timercraft.Enabled = true;
                    }
                }
                catch (Exception)
                {
                }
            }
     
            private void timercraft_Tick(object sender, EventArgs e)
            {
                if (!this.bgwcraft.IsBusy)
                {
                    this.bgwcraft.RunWorkerAsync();
                }
            }
     
            private void checkBox111_CheckedChanged(object sender, EventArgs e)
            {
                if (this.checkBox111.Checked)
                {
                    if (this.domainUpDown7.Text != "Select Item" || this.domainUpDown8.Text != "Select Item" || this.domainUpDown9.Text != "Select Item")
                    {
                        this.timercraft.Interval = 1000;
                        this.timercraft.Enabled = true;
                    }
                }
                else
                {
                    this.timercraft.Enabled = false;
                }
            }
     
            private void InitializeComponent()
            {
                .
                .
                .
                this.timercraft = new System.Windows.Forms.Timer(this.components);
                this.bgwcraft = new System.ComponentModel.BackgroundWorker();
                .
                .
                .
                // 
                // bgwcraft
                // 
                this.bgwcraft.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bgwcraft_DoWork);
                // 
                // timercraft
                // 
                this.timercraft.Tick += new System.EventHandler(this.timercraft_Tick);
                .
                .
                .
            }
        }

  3. #3
    Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2011
    Messages : 63
    Points : 47
    Points
    47
    Par défaut
    en passant : this.timercraft.Interval = this.crafttimer + 5000; en commentaire ça marche mais sur le timer de 1s
    Qu'es qui lui plaie pas dans ma redéfinition du timer ?

    crafttimer et un int ça valeur est en milliseconde
    je comprend vraiment pas la...

  4. #4
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 568
    Points
    3 568
    Par défaut
    Ca à avoir avec la popote interne de Form.Timer si tu redéfinis le timer sur un thread non UI.

    Essaye, dans le backgroundworker :
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
                        this.Invoke(new Action(() =>
                            {
                                this.timercraft.Interval = this.crafttimer + 5000; // Je rajoute 5s de marge pour être sur que ce soit bien finis
                                this.timercraft.Enabled = true;
                            }));
    Microsoft MVP : Windows Platform

    MCPD - Windows Phone Developer
    MCPD - Windows Developer 4

    http://www.guruumeditation.net

    “If debugging is the process of removing bugs, then programming must be the process of putting them in.”
    (Edsger W. Dijkstra)

  5. #5
    Membre du Club
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2011
    Messages : 63
    Points : 47
    Points
    47
    Par défaut
    MERCII
    Un tuto ou quelque chose que je pourrai lire pour comprendre le comment du pourquoi y faut faire un invoke ?

  6. #6
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 568
    Points
    3 568
    Par défaut
    Je ne saurais pas dire exactement pourquoi (Je ne suis pas (plus) un spécialiste Winform) mais le Form.Timer est lié au thread UI de la form (au dispatcher du thread UI, probablement). J'imagine que, si il ne trouve pas celui-ci (ce qui est le cas lorsqu'il est appellé depuis un backgroundworker), il ne peut pas s'initialiser correctement et donc ne démarre pas. Dommage qu'il ne mette pas un message d'erreur...

    Si quelqu'un a la réponse exacte, je suis aussi preneur
    Microsoft MVP : Windows Platform

    MCPD - Windows Phone Developer
    MCPD - Windows Developer 4

    http://www.guruumeditation.net

    “If debugging is the process of removing bugs, then programming must be the process of putting them in.”
    (Edsger W. Dijkstra)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 25/06/2014, 11h45
  2. Code avec propriétés dynamiques qui ne marche qu'une fois
    Par WernerDij dans le forum Général VBA
    Réponses: 2
    Dernier message: 17/06/2014, 10h17
  3. [AJAX] Panier ajax qui ne marche qu'une fois..
    Par dev14 dans le forum AJAX
    Réponses: 1
    Dernier message: 18/07/2013, 03h59
  4. condition qui ne marche qu'une fois sur deux
    Par guy59960 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 29/03/2010, 22h43
  5. Bouton ActiveX qui ne marche qu'une fois
    Par JeanMikael dans le forum VBA Access
    Réponses: 2
    Dernier message: 17/09/2007, 11h25

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