VB.NET and Threading

  • Thread starter Thread starter Chris Johnson
  • Start date Start date
C

Chris Johnson

Ill be the first to admit this is way beyond my current scope as a VB
programmer but Im learning as I go.

I playing with the new Threading features exposed by the 2.0 framework and
have writting a very simple application that executes three sequential
tasks, the second of which I send off on its on thread and it take a very
long time, so the order of completion should be Task1, Task3, Task2.

This all works, as astounding as I found that, and I am moving on to more
complex tasks with Threading, which leads me to:

When I send my second task on its on thread I use the following code:
myPingThread = New System.Threading.Thread(AddressOf myPingSub)
myPingThread.Start()

The problem here is that I cannot seem to pass an arguement to the myPingSub
as "addressof operand must be the name of a method without parentheses".

Can any of you more advanced VB guys assist me here in what Im overlooking?
How, when setting a function off on its on thread, can I pass it a value?

Thanks,
chrisj
 
Make myPingSub be a member function in a class, and given the class
data members. For instance:

Module TestModule
class MyPingClass
Public data As String

Public Sub myPingSub()
System.Console.Writeline("You passed: " & data
End Sub

End Class

Public Sub Main
Dim mpc As New MyPingClass()

mpc.data = "My data"

Dim myPingThread As System.Threading.Thread = New
System.Threading.Thread(AddressOf mpc.myPingSub)
myPingThread.Start()
myPingThread.Join()

End Sub
End Module
 
Chris,

You know that using threading does as well mean that you use more processing
time.

I can only give you a benefit in the throughput time, however therefore you
need parallel not depended processes.

I thought you did not know that when I saw your message.

Cor
 
For a brief process like I am currently executing I think your right, in
that there isn't much gain in multithreading a simple ping application.

But, I'm only using that as a learning block to figure out how to thread
applications.

My main goal is to modify a current personal project I have going that is a
web image scraper using the new 2.0 classes that allow quick download of an
image
given its URL (I forget the class name). The problem I ran into was that
whenever an image was being downloaded via my loop, the GUI became
unresponsive because they are both on the same system thread, so I need a
way to send the image download off to another thread.

Hopefully by starting small like I am I can get the requisite knowledge
about threading to then use in the aforementioned project and others in the
future.

Given the additional information above, if you have suggestions about
possible alternatives, feel free to let me know!
chrisj
 
Chris,

You are probably number 10001 who is doing this.

Have a look at the queue class to do this. As well know that HTTP has a
limitied ammount of connections by default. (You can tweak this) (that
scrapping you could do as well very quick in 1.x with
webclient.downloadfile).

However have a look at that queu class that makes live in this a lot easier.

http://msdn.microsoft.com/library/d...tml/frlrfsystemcollectionsqueueclasstopic.asp

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top