Bonjour,

je suis en train de tenter de dompter ce BackgroundWorker. Mais c'est pas encore moi qui ai le dessus.

Bref le DoWork s’exécute bien et ressemble à :

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
 
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
      WorkerParams Parametres = e.Argument as WorkerParams;
      //System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream("BackToFrontData");
        System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "BackToFrontData", PipeDirection.Out, PipeOptions.Asynchronous);
 
      pipeClient.Connect();
 
      serialPort1.PortName = Parametres.ComPort;
      serialPort1.BaudRate = Convert.ToInt32(Parametres.Speed);
      serialPort1.DataBits = 8;
      serialPort1.Parity = Parity.None;
      serialPort1.StopBits = StopBits.One;
      serialPort1.ReadTimeout = 50;
      serialPort1.Open();
 
      //backgroundWorker1.WorkerReportsProgress = true;
 
      if (serialPort1.IsOpen == false)
      {
        backgroundWorker1.CancelAsync();
      }
 
      do
      {
        byte[] data = { 0, 1 };
        try
        {
            data[0] = (Byte)serialPort1.ReadByte();
            data[1] = (Byte)serialPort1.ReadByte();
            pipeClient.Write(data, 0, 2);
            Debug.Print(data[0].ToString());
            //try
            //{
                //backgroundWorker1.ReportProgress(2);
                backgroundWorker1.ReportProgress(0);
                //Application.DoEvents();
            //}
            //catch (InvalidOperationException)
            //{
            //    Debug.Print("can't progress");
            //}
        }
        catch (TimeoutException)
        {
            String msg = "can't progress";
            Debug.Print(msg);
        }
      } while (true);
    }
Je peut y placer un breakpoint, tout se passe bien à ce niveau. En revanche la méthode ProgressChanged n'est jamais appelée.

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
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // This function fires on the UI thread so it's safe to edit
        // the UI control directly, no funny business with Control.Invoke.
        // Update the progressBar with the integer supplied to us from the
        // ReportProgress() function.  Note, e.UserState is a "tag" property
        // that can be used to send other information from the
        // BackgroundThread to the UI thread.
      Byte[] Cmd = { 0, 1 };
 
      pipeServer.Read(Cmd, 0, 2);
      //if (Cmd.ToString() == "EE")
      //{
      //    Byte[] Address = { 0, 1 };
      //    Byte[] Valeur = { 0, 1, 2, 3 };
 
      //    pipeServer.Read(Address, 0, 2);
      //    pipeServer.Read(Valeur, 0, 4);
 
      //    String query = "UPDATE eeprom SET Val=" + Address.ToString() + "WHERE Adr=" + Valeur.ToString();
 
      //    try
      //    {
      //       SQLiteDatabase db = new SQLiteDatabase("D:\\Dev\\Flair5XX\\DOC\\third\\Com\\F5xx_Com\\F5BD.s3db");
      //        DataTable F5DB;
      //        F5DB = db.GetDataTable(query);
      //        UpdateEEPROMtree();
      //    }
      //    catch (Exception fail)
      //    {
      //        String error = "The following error has occurred:\n\n";
      //        error += fail.Message.ToString() + "\n\n";
      //        MessageBox.Show(error);
      //        this.Close();
      //    }
      //    StepStatus = 1;
      //}
      //else
      //{
          // affichage BYTE
          richTextBox1.Text += Cmd[0].ToString("X2");
          richTextBox1.Text += " ";
          richTextBox1.Text += Cmd[1].ToString("X2");
          richTextBox1.Text += " ";
 
          // affichage WORD
          // richTextBox1.Text += Cmd.ToString("X4");
          // richTextBox1.Text += " ";
          StepStatus = 1;
      //}
  }
J'ai cherché sur les forums est j'ai donc essayé les cas cité, donc :
- Oui j'ai bien mis la propriété WorkerReportsProgress à true (par code et par la fenêtre Propriété de l'IDE)
- J'ai essayé d'appeler DoEvent pour "décongestionner" la file des Events
- j'ai tenté de voir si l'exception InvalidOperationException était levée mais non

Bref je suis sec, d'autant plus que j'apprends, j’espère avoir de l'aide pour continuer ....

Enfin j'ai remarqué qu'il y a plein de gens qui rament comme moi avec ce ReportProgress.

Sinon le reportProgress fonctionné dans cette appli, ce projet avant que je ne modifie le code (pour y intégrer une BD SQLite comme on peut le lire dans le code).

Cordialement