Thread & Window.Forms

vbnino Messages postés 218 Date d'inscription jeudi 13 juillet 2006 Statut Membre Dernière intervention 11 juillet 2010 - 9 sept. 2008 à 15:00
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 - 10 sept. 2008 à 14:42
Salut!

-J'ai dans ma Class Form1 ce code :
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
   Dim Ths As System.Threading.ThreadStart = New System.Threading.ThreadStart(AddressOfsearch)
        Dim Th As System.Threading.Thread = New System.Threading.Thread(Ths)
        Th.Priority = System.Threading.ThreadPriority.BelowNormal
        Th.IsBackground = True
        Th.Start()
        Do
            Th.Join(1)
            Application.DoEvents()
        Loop Until Not Th.IsAlive
end sub

Le probléme c'est que quand je clique sur le button il me renvoi une erreur :

  Cross-Thread operation not valid : Control 'rep' accessed from a thread other than
  the thread it was created on

rep= un control Listbox

-Merci de m'aider

3 réponses

NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
9 sept. 2008 à 15:28
Bonjour,

.NET interdit l'utilisation de controles par d'autres Threads que celui qui l'a créé.

Pour cela, utilises Un Delegate, puis pour lancer l'affichage, regarde Control.Invoke (control peut être une Form par exemple).

http://nhen0039.chez-alice.fr/index.php
0
vbnino Messages postés 218 Date d'inscription jeudi 13 juillet 2006 Statut Membre Dernière intervention 11 juillet 2010
10 sept. 2008 à 12:12
Salut!

-Un Délégate !!! , dois-je déclarer ma fonction search avec Délégate?

 Quand utilisé ...invoke ?

-Merci
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
10 sept. 2008 à 14:42
Bonjour,

Recherche "Delegate" dans MSDN et regarde l'aide sur Me.Invoke et là tu aura tout.

http://nhen0039.chez-alice.fr/index.php
0
Rejoignez-nous