[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Starting threads for my functions

Somewhat continued from
http:/Q_22477954.html

So, in my keyup event, I'm also acquiring the user's email address.  This calls a function (in my module class) that performs an ldap query to obtain that info.

This is how I accomplish it...
 txtTo.Text = FindEmail(txtShare.Text)

So, in order to get this into it's own thread as well, would I simply follow the past instructions, but place the delegate sub in the module?  (I know I could try before I ask, but I'm a bit lazy today)...

May need some guidance on this - it may be as simple as plugging in what I've previously used - so it could be super-easy points for somebody... :^)
0
sirbounty
Asked:
sirbounty
  • 2
  • 2
2 Solutions
 
jake072Commented:
The delegate can be in the module or in the form, but you will have to use it in the form (i.e., Invoke it in the form), because you need to invoke it in the proper thread context :)

Good luck, let me know if you need more help,

Jake
0
 
sirbountyAuthor Commented:
I guess I'm wondering how I would use the above...
Presumably I'd have to change it from
txtTo.Text=FindEmail(txtShare.text)
to something else...?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would place your FindEmail() function in a class that encapsulates a thread for you.  Then make that class raise a custom event with the return data.

This means that you can't do a direct assignment like are doing now but instead have to move to an event based model.

Public Class Form1

    Private Sub foo()
        Dim sc As New SomeClass
        AddHandler sc.SomeEvent, AddressOf ReturnedEmail
        sc.GetEmailAddress(txtShare.text)
    End Sub

    Private Delegate Sub EmailData(ByVal email As String)

    Private Sub ReturnedEmail(ByVal email As String)
        If Me.InvokeRequired() Then
            Me.Invoke(New EmailData(AddressOf ReturnedEmail), New Object() {email})
        Else
            txtTo.Text = email
        End If
    End Sub

    Public Class SomeClass
 
        Public Event SomeEvent(ByVal email As String)

        Private inData As String
        Private T As System.Threading.Thread

        Private Sub GetEmailAddress(ByVal inData As String)
            If T Is Nothing Then
                Me.inData = inData
                T = New System.Threading.Thread(AddressOf GetTheEmail)
                T.Start()
            End If      
        End Sub

        Private Sub GetTheEmail()
            ' get your email address...however YOU do it

            ' this is simulated work...
            System.Threading.Thread.Sleep(3000)

            ' return the result
            RaiseEvent SomeEvent("~~~Echo~~~ " & inData & " ~~~Echo~~~")
        End Sub

    End Class

End Class
0
 
jake072Commented:
Idle_Mind has illustrated how to use it... Sorry, I was lazy :)

Jake
0
 
sirbountyAuthor Commented:
Sorry for the delay IM - hope to try this out shortly...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now