Bonjour,

j'utilise des background worker pour éffectuer une recherche et ensuite afficher le résultat dans une datagrid. Chaqueque backgroundWorker appel la fonction AddJob ci dessous pour afficher de nouvelles infos dans ce datagrid.

Lorsque je fais ma première recherche, ceci se passe bien.

Lorsque je clique une seconde fois sur le bouton rechercher, j'obtiens le message d’erreur suivant :

Specified element is already the logical child of another element. Disconnect it first.
Ceci apparait au niveau de l'utilisation du dispatcher.

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
 
/// <summary>
        /// Add a new Jod to the Datagrid
        /// </summary>
        /// <param name="target">Inform the user of the target of this job</param>
        /// <param name="description">Description of the process running</param>
        /// <returns></returns>
        public Job AddJob(string target, string description)
        {
            //Use the dispatcher  cause this function may be called from a thread, therefore it has to be executed in the Main UI thread
            //If no dispatcher = exception
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                      delegate()
                      {
                          iJobInstanceID += 1;
                          _ocJobs.Add(new Job(iJobInstanceID, target, description));
                      }
                ));
 
            return _ocJobs.Last();
        }
        }
Je suppose que cette erreur vient d'un thread mal "fermé" ? mais je n'arrive pas à voir comment m'assurer que tout les thread utilisées soient correctement détruits à la fin.

Merci pour votre aide