0


I need some more help. I am a noob at threading and I am making an application on
vb.net.
Basically, what my application does is it searches in Lotus Notes for information that
I need, while it is searching for the information, i need a Gif to display some
animation. I do not want to use backgroundworker for constraint reasons. So I need to
use threads.


This is the code I have


  Private Sub btnRechercheUtilisateur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercheUtilisateur.Click
        Dim t As New Thread(New ThreadStart(AddressOf RunInThread))
        t.Start()
    End Sub

Public Delegate Sub MyDelegate(ByVal arg As Integer)
    Private Sub RunInThread()
        Dim delInstatnce As New MyDelegate(AddressOf RechercheUtilisateurView)
        Me.BeginInvoke(delInstatnce)
        imgLoading.Visible = True
        'Add your code that needs to be executed in separate thread 
        'except UI updation
    End Sub


Public Sub RechercheUtilisateurView()
        'Vérifie si la session est valide
        If notes.IsSessionValide = False Then
            LoginPanel.Visible = True
            Exit Sub
        End If

    'Vide la liste
    lstSearchUsager.Items.Clear()
    lstGroupesUsager.Items.Clear()

    'Uncheck checbox 
    chkGroupesUsager.Checked = False

    'UI Setting pour le loading
    ' StartLoadingImg(172, 152)
    txtSearchUsager.Enabled = False
    btnRechercheUtilisateur.Enabled = False

    'Ajoute les éléments de la liste
    Dim users As List(Of UsagerNotes) = notes.GetUsagersByKeyword(txtSearchUsager.Text)

    'Vérifier si un résultat a été retourné
    If users.Count <> 0 Then
        Dim rows(users.Count - 1) As ListViewItem
        Dim counter As Integer = 0
        'Loop d'ajout d'utilisateur au ListView
        For Each u In users
            Dim row As New ListViewItem
            row.Text = u.nomCanonique
            row.ImageKey = "1"
            rows(counter) = row
            counter += 1
        Next
        lstSearchUsager.Items.AddRange(rows)

    Else

        AddEvent("Aucun résultat trouvé pour la recherche utilisateur: " + txtSearchUsager.Text, "21")

    End If

        txtSearchUsager.Enabled = True
    btnRechercheUtilisateur.Enabled = True
  End Sub

When i use the debugger, my code would freeze at around txtSearchUsager.Enabled = False Can anyone please help me, I am confused

Thanks Gibit

GIBIT
  • 97
  • 1
  • 2
  • 13
  • 2
    What "constraint reasons" are you referring to? This looks like a near perfect usage of BackgroundWorker. (not arguing with your decision, just trying to understand it so I can approach the problem with that in mind) – Joel Etherton Dec 10 '10 at 15:55
  • oh just some constraints by my clients. The thing is my application will probably have much more searches done in notes, and we dont want to add a background worker for each and every search. We will use much more threading – GIBIT Dec 10 '10 at 15:59
  • What's wrong with multiple background workers? I expect it will still be the easiest way to manage multiple searches. If you have VB 2010 you can declare the event handlers inline as lambdas. One routine could hold all the code for one search. – MarkJ Dec 11 '10 at 12:38

1 Answers1

1

You must include all function calls (include change the enabled property of the textbox) in a separate function and call this function over a delegate with Me.Invoke or Me.BeginInvoke.

wickie79
  • 470
  • 1
  • 3
  • 7