Bonjour,
j'utilise des background worker pour éffectuer une recherche et ensuite afficher le résultat dans une datagrid. Chaque 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 (recharge une nouvelle datagrid pour une nouvelle recherche), j'obtiens le message d’erreur suivant :
Ceci apparait au niveau de l'utilisation du dispatcher (methode addJob).Specified element is already the logical child of another element. Disconnect it first.
Je suppose que l'utilisation de RemoveLogicalChild ou RemoveVisualChild peut aider, mais je ne voit pas comment l'utiliser.
Ci dessous le code. Merci pour votre aide !
Code qui utilise un backgroud worker. Chaque background worker est censé créer une ligne dans ma datagrid via PageExecutionJob.JbList.AddJob
lors de l'appel à PageExecutionJob.JbList.AddJob, ceci met à jour une une datagrid (via une observable collection)
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 NavigationWindow popup = new NavigationWindow(); List<SCCMAdvertisement> lAdvFound = new List<SCCMAdvertisement>(); //Open the job summary window popup.Show(); popup.Navigate(PageExecutionJob); //List of BW that will containt the different BW created for the search of each site List<BackgroundWorker> lBackJob = new List<BackgroundWorker>(); // Will start to search on selected promary Sites foreach (SCCMSite site in lbSccmSites.SelectedItems) { //Create a thread for each site connection lBackJob.Add(new BackgroundWorker()); //Get the latest BW from the list (1BW = 1SCCM Site to search) BackgroundWorker bwLastGenerated = lBackJob.Last(); bwLastGenerated.WorkerSupportsCancellation = true; //Define the job to be done for each BW bwLastGenerated.DoWork += delegate(object s, DoWorkEventArgs args) { SCCMSite oBwSite = (SCCMSite)args.Argument; //Display a new job in the Job List PageExecutionJob.JbList.AddJob(oBwSite.Name, "Connection to WMI Database");
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 /// <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 //ERREUR ICI QUAND JE RELANCE UNE SECONDE RECHERCHE Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate() { iJobInstanceID += 1; _ocJobs.Add(new Job(iJobInstanceID, target, description)); } )); return _ocJobs.Last(); } }
Partager