Passage de paramètre à partir d'un Timer ver une Form

j78330 Messages postés 10 Date d'inscription mardi 17 septembre 2013 Statut Membre Dernière intervention 18 juillet 2016 - Modifié par BunoCS le 2/06/2016 à 17:51
 j78330 - 2 juin 2016 à 20:42
Bonjour,

J'ai un problème depuis plus une semaine, je déclare et lance un Timer dans une classe static , pour ensuite passer un paramètre dans une Form à l'aide d'une méthode .
Et j'ai cette erreur : Opération inter-threads non valide : le contrôle 'lblGainDon1' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.

Pouvez-vous m'aidez s'il vous plait ? En vous remerciant avance

///////////////////////////////////////////////////////////////////////////////////////////////////

Voici ma class Progrma.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComAD
{
    static class Program
    {
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           
            principale_ = new Principale();
           
            gain = new Gain();
          
            
      
   
            Application.Run(principale_);
        }

       

        private static Gain gain;
        public static Gain gain_ { get { return gain; } set { gain = value; } }

        private static Principale principale;
        public static Principale principale_ { get { return principale; } set { principale = value; } }
    }
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////

Voici une partie de ma form prinicipale ou j'ouvre ma form Gain
private void Boutonconfigurer_Click(object sender, EventArgs e)
{

Program.gain_.Show();
}


////////////////////////////////////////////////////////////////
Voici ma class static qui s'appelle Com

using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using System.Timers;
using System.Drawing;


namespace ComAD 
{

   public static class Com 
    {
       static byte[] c = new byte[100];
       static byte[] b = new byte[100];
       public static byte[] x = new byte[100];
        
         public static int Demande;
 
         public static System.Timers.Timer timerInterrogation;
        private delegate void messagecombobox(string lstInformations);

        public static void Initialisation()
        {
            timerInterrogation = new System.Timers.Timer();
            timerInterrogation.Elapsed += new ElapsedEventHandler(timerInterrogation_Elapsed);
            timerInterrogation.Interval = 1000;
            timerInterrogation.Enabled = true;
        } 
        public static void timerInterrogation_Elapsed(object sender, ElapsedEventArgs e)
        {
            
            switch (Demande)
            {
                case 0:
               
                    Program.gain_ .DonneeGain(Demande);
                    Demande = 1;
                    break;

                    case 1 :

                   // instructions

                     break;

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////

Voici une partie du code de de ma form (qui s'appelle Gain) ou je passe le paramètre


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 ComAD
{
    public partial class Gain : Form
    {
        // Instructions
       public double Num;

        public Gain()

           InitializeComponent();

              // Instructions


                public void DonneeGain(int Demande)
                          {
                                   Num = (Com.x[7] * 256) + Com.x[8];


                                     Program.gain_.lblGainDon1.Text= Num.ToString();  // lblGainDon1.Text est une textBox de la form Gain l'erreur se trouve à cette ligne
                            }

                     // Instructions
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////


EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

2 réponses

Whismeril Messages postés 19025 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 19 avril 2024 656
2 juin 2016 à 18:21
Bonjour,

il faut rendre ton traitement threadsafe.

Dans ton timer, ou dans tout thread qui n'est pas le principal, il faut utiliser le Dispatcher.

Par exemple si tu veux laisser au programme la gestion des priorités:
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
                {
                    //code qui parle à un autre thread
                    Program.gain_ .DonneeGain(Demande);//dans ton cas je pense
                }
                ))

}

0
Merci je vais essayer sa
0
Rejoignez-nous