Solved

Starting threads for my functions

Posted on 2007-03-28
5
226 Views
Last Modified: 2010-04-23
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
Comment
Question by:sirbounty
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:jake072
jake072 earned 50 total points
Comment Utility
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
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 450 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:jake072
Comment Utility
Idle_Mind has illustrated how to use it... Sorry, I was lazy :)

Jake
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
Sorry for the delay IM - hope to try this out shortly...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now