How to thread a function as array (or better)

T

TommyB

Hi NG,
first of all sry about my english :) maybe it's somethin' funny to you

Following situation:

I've a structure like this:

Structure _Test
Dim IP as String
Dim UserLogin as String
End Structure
Dim Test() as _Test

This should keep the given information in an array, where each array-item is
used for one ip in local network.

In the main code the structure goes initialized like this:

Redim Test(2)
Test(0) = New _Test
Test(1) = New _Test
Test(2) = New _Test
Test(0).IP = "192.168.0.1"
Test(1).IP = "192.168.0.10"
Test(2).IP = "192.168.0.100"

Now I'll move my structure through a function.

Test = ProcessStruct(Test)

This structure does the following:
It calls for each item another function, witch is processing the ip and gets
back the Username. This is written in the structure. On End the actualized
structure is returning.

And all works fine, but:
When I want to scan the complete subnet-range (ie 192.168.0.x) it could hang
my program up to 4sec per ip, when function gets timeout.
Now I want to use threads, but I've no idea how to passing the structure
through the thread.
I think it must be possible, that the main function calls the 2nd function
(witch retrive information) all 100ms (for example) with the next ip. The
main function should gets the returned information and assemble it to the
structure.
But how to do it? I tried with events, but withevents wont work with arrays.
Now I'm confused and need help or other examples, how to do it better, than
I thougt.

Thanks and please help!
 
G

Guest

You should be able to put your thread sub in the same class or module and it
will have access to your test array. If you are going to access your array
in your class while the threaded sub is running, you will want to use
SyncLock on the code that modifies the test array.
 
C

Cor Ligthert

TommyB,

What do you think that the result from a thread will be.

I know at least one, It will take some more time to get the IP address.

However when you want to test them all together in the same logical time,
than you can gain some performance when this is a kind of ping operation.

Use than a Queue wherin you have your structures.

Here in this message is a little sample of that from me with two assynchroon
working threads which give back the results.

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/1aab0525a15afdb6?hl=en

I hope this helps,

Cor
 
T

TommyB

Many thanks for that!
Well, I think my question wasn't very good formed, sorry for that.
I know, if I call all requests at one time, they'll wont work as they're
developed for and could hanging my System.
Well, I don't want to run all threads like this. I thougt before it's
possible to create an array of threads with a bound from 0 to 9 (10 threads)
witch are calling the same function. This threads should started like this:

Do While RequestsRemaining > 0
For intCnt = 0 to 9
If tWorker(intCnt).State <> Running Then ' Start the Thread only
if previous process is finish (not in Syntax)
tWorker(intCnt).??? = ??? ' Gives the thread the IP, that
should be processed in it
tWorker(intCnt).Start ' Starts the thread
Thread.Sleep(100) ' Hold this thread für
100ms. Should be enough for sending the request
' without
waiting for the answer
End If
Next
Loop
' Jump out if all Requests are send
RaiseEvent CollectDataFinished

True, not coded out, because I don't know how, but for visualation it should
be enough.
Fill the Threads with the Row-Number of the structure and start for
processing it, if it's not already running.
Run with a maximum of 10 open requests at one time and each new process
should keep the next thread 100ms to wait for. This should be enough to send
the Request to the Client. If an answer goes back to the thread (real answer
or timeout-message), it'll put the data to the given row on the structure
and raise an event, so the main code can see, there are changes.
Well, I try now to understand your code and use it to help me out. It's
clearly typed, but I had never before seen the 'Quere' type. ('Private myQ
As New Queue')
 
T

TommyB

Addition: What must I import to use the Quere like in your example?
VB highlights it as error and it's not in the list appears alter typing the
new statement...
 
T

TommyB

hmmm, i had to do import 'System.Collections'.
Thanks for all, I will now test and use it :)
CYa!
 

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

Top