need some help multithreading a function

droidman

Member
Joined
Apr 4, 2011
Messages
5
Programming Experience
3-5
hi, i need to create multiple threads to improve a function on my program. it is a simple thing really but it's slow and if multithreaded it can improve the speed some thousand times.
my program uses a dll to check the MX record of an e-mail from a textbox called emails
if the mx record is correct then it validates that domain, if not, it sends to an error textbox with that mail
my problem is that im reading a lot on multithreading and i dont have an idea how to make something like this:
for x=0 to emails.lines.count -1
' start a thread that runs my function
next

if i can limit the number of threads to avoid crashing it would be good.
and that's pretty much it.
help?
thanks in advance :smile:
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
The fact that you're using .NET 4.0 means that your job is simplified significantly by the new Tasks library, e.g.
Parallel.ForEach(someList, AddressOf DoSomething)
That basically takes the place of a For Each loop where what would previously have been the body of the loop is now extracted out into the DoSomething method:
Private Sub DoSomething(data As Object)
    'Process data here
End Sub
Parallel.ForEach will process multiple items simultaneously up to a limit, with additional items queued until a thread become available. Once all items have been processed, the ForEach method returns. You have many more options than that in the Tasks library too. If you think you need more, start by reading the documentation for the System.Threading.Tasks namespace.
 

droidman

Member
Joined
Apr 4, 2011
Messages
5
Programming Experience
3-5
ok mate thank you for your reply
i tried this with little success:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim N As Integer = Me.emails.Lines.Count - 1
Parallel.For(0, N, Sub(i, loopState)
Me.results.Text = Me.results.Text & Me.emails.Lines(i) & vbNewLine
Console.WriteLine(i)
If i = 100 Then
'loopState.Break()
End If


End Sub)
End Sub

this is just to test if the values from emails.text go to results.text
what happens is that just like 2 ou 3 values, randomly go to results.text
if i enter in the emails listbox this:
1
2
3
4
5
and then press button3 what i get in the results.text is:
1
4
or
1
3
etc
i have no idea what is goin on
i just wanted to improve my function
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,545
Location
Norway
Programming Experience
10+
Each Parallel.For iteration is executed on a secondary thread, so that code can't possibly generate any output, since accessing UI controls will throw exceptions. To do that you can get synchronization context from TaskScheduler and use that to do some UI related action. For example:
Dim sync = TaskScheduler.FromCurrentSynchronizationContext
Dim N As Integer = Me.Emails.Lines.Count 
Parallel.For(0, N, Sub(i)
                       Debug.WriteLine(i)
                       Dim ui As New Task(Sub() Me.Results.AppendText(Me.Emails.Lines(i) & vbNewLine))
                       ui.Start(sync)
                   End Sub)

Notice also For methods first two parameters are fromInclusive and toExclusive.
 

droidman

Member
Joined
Apr 4, 2011
Messages
5
Programming Experience
3-5
hmm ok i'm starting to understand this. but for example, if i modify this line:
"Dim ui As New Threading.Tasks.Task(Sub() Me.Results.AppendText(Me.Emails.Lines(i) & vbNewLine))"
i get a lot of overflow errors. so what do you think of using the thread to call a function ?
can i do that ? and how?
thanks again
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,545
Location
Norway
Programming Experience
10+
OverflowException is thrown when attempting to assign a variable a value that is outside the range of the destination type.
so what do you think of using the thread to call a function ?
Any valid call can be made from any thread.
 
Top Bottom